Fortran 程序
程序是一组执行明确任务的语句,可以从程序中调用。信息(或数据)作为参数传递给调用程序。
有两种类型的程序:
- Functions
- Subroutines
函数
函数是一个返回单个数量的过程。函数不应修改其参数。
返回的数量称为函数值,由函数名表示。
语法
function name(arg1, arg2, ....)[declarations, including those for the arguments][executable statements]end function [name]
以下实例演示了一个名为 area_of_circle 的函数,它计算半径为 r 的圆的面积:
program calling_funcreal :: aa = area_of_circle(2.0)Print *, "The area of a circle with radius 2.0 is"Print *, aend program calling_func! this function computes the area of a circle with radius rfunction area_of_circle (r)! function resultimplicit none! dummy argumentsreal :: area_of_circle! local variablesreal :: rreal :: pipi = 4 * atan (1.0)area_of_circle = pi * r**2end function area_of_circle
结果为:
The area of a circle with radius 2.0 is12.5663710
注意
- 您必须在主程序和过程中都指定隐式 none。
- 被调用函数中的参数 r 称为伪参数。
结果选项
如果希望将返回值存储在函数名以外的其他名称中,可以使用 result 选项。
您可以将返回变量名称指定为:
function name(arg1, arg2, ....) result (return_var_name)[declarations, including those for the arguments][executable statements]end function [name]
子程序
子程序不返回值,但可以修改其参数。
语法
subroutine name(arg1, arg2, ....)[declarations, including those for the arguments][executable statements]end subroutine [name]
调用子程序
您需要使用 call 语句调用子程序。
以下实例演示了更改其参数值的子程序交换的定义和使用。
program calling_funcimplicit nonereal :: a, ba = 2.0b = 3.0Print *, "Before calling swap"Print *, "a = ", aPrint *, "b = ", bcall swap(a, b)Print *, "After calling swap"Print *, "a = ", aPrint *, "b = ", bend program calling_funcsubroutine swap(x, y)implicit nonereal :: x, y, temptemp = xx = yy = tempend subroutine swap
结果为:
Before calling swapa = 2.00000000b = 3.00000000After calling swapa = 3.00000000b = 2.00000000
指定参数的 intent 属性
intent 属性让您可以指定在过程中使用参数的意图。下表提供了 intent 属性的值:
| 值 | 使用 | 说明 |
|---|---|---|
| in | intent(in) | 用作输入值,不在函数中更改 |
| out | intent(out) | 用作输出值时,它们会被覆盖 |
| inout | intent(inout) | 参数既被使用又被覆盖 |
比如下面的实例:
program calling_funcimplicit nonereal :: x, y, z, discx = 1.0y = 5.0z = 2.0call intent_example(x, y, z, disc)Print *, "The value of the discriminant is"Print *, discend program calling_funcsubroutine intent_example (a, b, c, d)implicit none! dummy argumentsreal, intent (in) :: areal, intent (in) :: breal, intent (in) :: creal, intent (out) :: dd = b * b - 4.0 * a * cend subroutine intent_example
结果为:
The value of the discriminant is17.0000000
递归程序
当编程语言允许您调用同一函数内的函数时,就会发生递归。这被称为函数的递归调用。
当一个过程直接或间接地调用自身时,称为递归过程。您应该在声明递归一词之前声明这类过程。
当递归使用函数时,必须使用 result 选项。
以下是一个实例,它使用递归过程计算给定数字的阶乘:
program calling_funcimplicit noneinteger :: i, fi = 15Print *, "The value of factorial 15 is"f = myfactorial(15)Print *, fend program calling_func! computes the factorial of n (n!)recursive function myfactorial (n) result (fac)! function resultimplicit none! dummy argumentsinteger :: facinteger, intent (in) :: nselect case (n)case (0:1)fac = 1case defaultfac = n * myfactorial (n-1)end selectend function myfactorial
内部程序
当程序中包含一个程序时,它被称为程序的内部程序。包含内部程序的语法如下:
program program_nameimplicit none! type declaration statements! executable statements. . .contains! internal procedures. . .end program program_name
实例如下:
program mainprogimplicit nonereal :: a, ba = 2.0b = 3.0Print *, "Before calling swap"Print *, "a = ", aPrint *, "b = ", bcall swap(a, b)Print *, "After calling swap"Print *, "a = ", aPrint *, "b = ", bcontainssubroutine swap(x, y)real :: x, y, temptemp = xx = yy = tempend subroutine swapend program mainprog
结果如下:
Before calling swapa = 2.00000000b = 3.00000000After calling swapa = 3.00000000b = 2.00000000