FORTRAN過程

2018-12-12 14:24 更新

過程是一組執(zhí)行一個(gè)明確的任務(wù),可以從你的程序中調(diào)用語句。信息(或數(shù)據(jù))被傳遞給調(diào)用的程序,向步驟作為參數(shù)。

有兩種類型的程序:

  • 功能
  • 子程序

功能

函數(shù)是返回一個(gè)單一的量的過程。函數(shù)不應(yīng)該修改其參數(shù)。

返回的數(shù)量被稱為函數(shù)值 ,并且它由函數(shù)名表示。

句法:

語法功能如下:

function name(arg1, arg2, ....)  
   [declarations, including those for the arguments]   
   [executable statements] 
end function [name]

下面的示例演示一個(gè)名為功能area_of_circle。它計(jì)算半徑為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

當(dāng)你編譯和執(zhí)行上面的程序,它會產(chǎn)生以下結(jié)果:

The area of a circle with radius 2.0 is
   12.5663710   

請注意:

  • 必須指定在兩個(gè)主程序隱無以及程序。

  • 在被調(diào)用函數(shù)的參數(shù)r被稱為偽參數(shù) 。

結(jié)果選項(xiàng)

如果你想返回的值存儲在其它一些名稱與功能名稱,您可以使用結(jié)果選項(xiàng)。

您可以指定返回變量名:

function name(arg1, arg2, ....) result (return_var_name)  
   [declarations, including those for the arguments]   
   [executable statements] 
end function [name]

子程序

子程序沒有返回值,但可以修改其參數(shù)。

句法

subroutine name(arg1, arg2, ....)    
   [declarations, including those for the arguments]    
   [executable statements]  
end subroutine [name]

調(diào)用子程序

您需要使用呼叫語句調(diào)用的子程序。

下面的例子演示了一個(gè)子程序交換,改變其參數(shù)值的定義和使用。

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

當(dāng)你編譯和執(zhí)行上面的程序,它會產(chǎn)生以下結(jié)果:

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

指定參數(shù)的意圖

這樣做的目的屬性允許你指定與參數(shù)在程序中使用的意圖。下表提供的意圖屬性的值:

作為說明
意圖(中) 用作輸入值,而不是在功能改變
意圖(出) 用作輸出值,它們都將被覆蓋
進(jìn)出意圖(INOUT) 參數(shù)都是使用和覆蓋

下面的例子演示了這個(gè)概念:

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

當(dāng)你編譯和執(zhí)行上面的程序,它會產(chǎn)生以下結(jié)果:

The value of the discriminant is
   17.0000000    

遞歸過程

遞歸當(dāng)編程語言,您可以調(diào)用同一個(gè)函數(shù)內(nèi)的函數(shù)發(fā)生。這就是所謂的函數(shù)的遞歸調(diào)用。

當(dāng)一個(gè)過程調(diào)用本身,直接或間接地被稱為遞歸過程。你應(yīng)該通過其聲明之前遞歸字前面的聲明這種類型的程序。

當(dāng)一個(gè)函數(shù)被遞歸使用, 結(jié)果選項(xiàng)被使用。

以下是一個(gè)例子,它計(jì)算階乘用于使用遞歸過程的給定數(shù)量:

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

內(nèi)部程序

當(dāng)一個(gè)過程被包含在程序中,它被稱為程序的內(nèi)部程序。包含的內(nèi)部程序的語法如下:

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

下面的例子演示了這個(gè)概念:

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   

當(dāng)你編譯和執(zhí)行上面的程序,它會產(chǎn)生以下結(jié)果:

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

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號