过程是一组执行一个明确定义的任务,可以从程序调用语句。信息(或数据)被传递给调用程序,以过程作为参数。
有两种类型的程序:
函数
子程序
函数
函数是返回一个数量的过程。函数不修改其参数。
返回数值被称为函数值,并将其表示为函数名。
语法:
函数的语法如下:
function name(arg1, arg2, ....)
[declarations, including those for the arguments]
[executable statements]
end function [name]
下面的示例演示一个函数名为area_of_circle。它计算半径为 r 的圆的面积。
program calling_func
real :: a
a = area_of_circle(2.0)
Print *, "The area of a circle with radius 2.0 is"
Print *, a
end program calling_func
! this function computes the area of a circle with radius r
function area_of_circle (r)
! function result
implicit none
! dummy arguments
real :: area_of_circle
! local variables
real :: r
real :: pi
pi =