Fortran過程

過程是一組執行一個明確定義的任務,可以從程式調用語句。資訊(或數據)被傳遞給調用程式,以過程作為參數。

有兩種類型的程式:

  • 函數
  • 副程式

函數

函數是返回一個數量的過程。函數不修改其參數。

返回數值被稱為函數值,並將其表示為函數名。

語法:

函數的語法如下:

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 = 4 * atan (1.0)
   area_of_circle = pi * r**2

end function area_of_circle

當編譯並執行上述程式,它會產生以下結果:

The area of a circle with radius 2.0 is
   12.5663710

請注意:

  • 必須指定隱含都不在這兩個在主程序和過程中。

  • 在被調用函數的參數r被稱為 dummy argument.

結果選項

如果想返回的值存儲在函數名的其他名稱,則可以使用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_func
implicit none

   real :: a, b
   a = 2.0
   b = 3.0

   Print *, "Before calling swap"
   Print *, "a = ", a
   Print *, "b = ", b

   call swap(a, b)

   Print *, "After calling swap"
   Print *, "a = ", a
   Print *, "b = ", b

end program calling_func


subroutine swap(x, y)
implicit none

   real :: x, y, temp

   temp = x
   x = y
   y = temp

end subroutine swap

當編譯並執行上述程式,它會產生以下結果:

Before calling swap
a = 2.00000000
b = 3.00000000
After calling swap
a = 3.00000000
b = 2.00000000

指定參數的意圖

意圖屬性允許指定與參數的過程中使用的意向。下表提供intent屬性的值:

使用為 解釋
in intent(in) 用作輸入值,而不是在函數中改變
out intent(out) 用作輸出值,它們將被覆蓋
inout intent(inout) 參數都使用和覆蓋

下麵的例子演示了這一概念:

program calling_func
implicit none

   real :: x, y, z, disc

   x= 1.0
   y = 5.0
   z = 2.0

   call intent_example(x, y, z, disc)

   Print *, "The value of the discriminant is"
   Print *, disc

end program calling_func


subroutine intent_example (a, b, c, d)
implicit none

   ! dummy arguments
   real, intent (in) :: a
   real, intent (in) :: b
   real, intent (in) :: c
   real, intent (out) :: d

   d = b * b - 4.0 * a * c

end subroutine intent_example

當編譯並執行上述程式,它會產生以下結果:

The value of the discriminant is
   17.0000000

遞歸過程

遞歸發生在一個編程語言可以調用同一個函數在函數內。這就是所謂的函數的遞歸調用。

當一個過程調用本身,直接或間接地被稱為遞歸過程。應該通過其聲明之前的字前面遞歸聲明這種類型的程式。

當一個函數被遞歸使用,則 result 選項要被使用。

以下是一個例子,它計算階乘用於使用一個遞歸過程:

program calling_func
implicit none

   integer :: i, f
   i = 15

   Print *, "The value of factorial 15 is"
   f = myfactorial(15)
   Print *, f

end program calling_func

! computes the factorial of n (n!)
recursive function myfactorial (n) result (fac)
! function result
implicit none

   ! dummy arguments
   integer :: fac
   integer, intent (in) :: n

   select case (n)
      case (0:1)
         fac = 1
      case default
         fac = n * myfactorial (n-1)
   end select

end function myfactorial

內部過程

當一個過程被包含在程式中,它被稱為程式的內部程式。包含一個內部程式的語法如下:

program program_name
   implicit none
   ! type declaration statements
   ! executable statements
   . . .
   contains
   ! internal procedures
   . . .
end program program_name

下麵的例子演示了這一概念:

program mainprog
implicit none

   real :: a, b
   a = 2.0
   b = 3.0

   Print *, "Before calling swap"
   Print *, "a = ", a
   Print *, "b = ", b

   call swap(a, b)

   Print *, "After calling swap"
   Print *, "a = ", a
   Print *, "b = ", b

contains
   subroutine swap(x, y)
      real :: x, y, temp
      temp = x
      x = y
      y = temp
   end subroutine swap

end program mainprog

當編譯並執行上述程式,它會產生以下結果:

Before calling swap
a = 2.00000000
b = 3.00000000
After calling swap
a = 3.00000000
b = 2.00000000  

上一篇: Fortran檔輸入輸出 下一篇: Fortran模組