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   

請注意:

  • 您必須在主程式和過程中都指定implicit none

  • 被呼叫函式中的引數 r 稱為虛擬引數

result 選項

如果希望返回值儲存在與函式名不同的名稱中,可以使用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語句來呼叫子程式。

以下示例演示了子程式 swap 的定義和使用,該子程式更改其引數的值。

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 屬性允許您指定在過程中使用引數的意圖。下表提供了 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    

遞迴過程

當程式語言允許您在同一個函式內部呼叫函式時,就會發生遞迴。這稱為函式的遞迴呼叫。

當一個過程直接或間接地呼叫自身時,稱為遞迴過程。您應該透過在其宣告前加上recursive關鍵字來宣告此類過程。

當函式以遞迴方式使用時,必須使用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   
廣告

© . All rights reserved.