Fortran - 模組



模組就像一個包,您可以將函式和子程式放在其中,如果您正在編寫一個非常大的程式,或者您的函式或子程式可以在多個程式中使用。

模組為您提供了一種在多個檔案之間分割程式的方法。

模組用於:

  • 打包子程式、資料和介面塊。

  • 定義可被多個例程使用的全域性資料。

  • 宣告可以在您選擇的任何例程中使用的變數。

  • 匯入整個模組以在另一個程式或子程式中使用。

模組語法

模組由兩部分組成:

  • 語句宣告的規範部分
  • 包含子程式和函式定義的包含部分

模組的一般形式為:

module name     
   [statement declarations]  
   [contains [subroutine and function definitions] ] 
end module [name]

在程式中使用模組

您可以透過 use 語句將模組合併到程式或子程式中:

use name  

請注意

  • 您可以根據需要新增任意數量的模組,每個模組都將放在單獨的檔案中並單獨編譯。

  • 一個模組可以在各種不同的程式中使用。

  • 同一個程式可以多次使用同一個模組。

  • 在模組規範部分宣告的變數對於該模組是全域性的。

  • 在模組中宣告的變數在使用該模組的任何程式或例程中都成為全域性變數。

  • use 語句可以出現在主程式或任何其他使用在特定模組中宣告的例程或變數的子程式或模組中。

示例

以下示例演示了該概念:

module constants  
implicit none 

   real, parameter :: pi = 3.1415926536  
   real, parameter :: e = 2.7182818285 
   
contains      
   subroutine show_consts()          
      print*, "Pi = ", pi          
      print*,  "e = ", e     
   end subroutine show_consts 
   
end module constants 


program module_example     
use constants      
implicit none     

   real :: x, ePowerx, area, radius 
   x = 2.0
   radius = 7.0
   ePowerx = e ** x
   area = pi * radius**2     
   
   call show_consts() 
   
   print*, "e raised to the power of 2.0 = ", ePowerx
   print*, "Area of a circle with radius 7.0 = ", area  
   
end program module_example

編譯並執行上述程式後,將產生以下結果:

Pi = 3.14159274    
e =  2.71828175    
e raised to the power of 2.0 = 7.38905573    
Area of a circle with radius 7.0 = 153.938049   

模組中變數和子程式的可訪問性

預設情況下,模組中的所有變數和子程式都透過use語句提供給使用模組程式碼的程式。

但是,您可以使用privatepublic屬性來控制模組程式碼的可訪問性。當您將某些變數或子程式宣告為private時,它在模組外部不可用。

示例

以下示例說明了這個概念:

在上一個示例中,我們有兩個模組變數,epi。讓我們將它們設為private並觀察輸出:

module constants  
implicit none 

   real, parameter,private :: pi = 3.1415926536  
   real, parameter, private :: e = 2.7182818285 
   
contains      
   subroutine show_consts()          
      print*, "Pi = ", pi          
      print*, "e = ", e     
   end subroutine show_consts 
   
end module constants 


program module_example     
use constants      
implicit none     

   real :: x, ePowerx, area, radius 
   x = 2.0
   radius = 7.0
   ePowerx = e ** x
   area = pi * radius**2     
   
   call show_consts() 
   
   print*, "e raised to the power of 2.0 = ", ePowerx
   print*, "Area of a circle with radius 7.0 = ", area  
   
end program module_example

編譯並執行上述程式後,將顯示以下錯誤訊息:

   ePowerx = e ** x
   1
Error: Symbol 'e' at (1) has no IMPLICIT type
main.f95:19.13:

   area = pi * radius**2     
   1
Error: Symbol 'pi' at (1) has no IMPLICIT type

由於epi都被宣告為private,程式module_example無法再訪問這些變數。

但是,其他模組子程式可以訪問它們:

module constants  
implicit none 

   real, parameter,private :: pi = 3.1415926536  
   real, parameter, private :: e = 2.7182818285 
   
contains      
   subroutine show_consts()          
      print*, "Pi = ", pi          
      print*, "e = ", e     
   end subroutine show_consts 
   
   function ePowerx(x)result(ePx) 
   implicit none
      real::x
      real::ePx
      ePx = e ** x
   end function ePowerx
    
   function areaCircle(r)result(a)  
   implicit none
      real::r
      real::a
      a = pi * r**2  
   end function areaCircle
    
end module constants 


program module_example     
use constants      
implicit none     

   call show_consts() 
   
   Print*, "e raised to the power of 2.0 = ", ePowerx(2.0)
   print*, "Area of a circle with radius 7.0 = ", areaCircle(7.0)  
   
end program module_example

編譯並執行上述程式後,將產生以下結果:

Pi = 3.14159274    
e = 2.71828175    
e raised to the power of 2.0 = 7.38905573    
Area of a circle with radius 7.0 = 153.938049   
廣告