鍍金池/ 教程/ Java/ Fortran模塊
Fortran還原功能
Fortran關系運算符
Fortran運算符優(yōu)先級
Fortran基本語法
Fortran文件輸入輸出
Fortran嵌套select case結構
Fortran變量
Fortran Cycle語句
Fortran語言環(huán)境設置
Fortran數(shù)據(jù)類型
Fortran數(shù)組
Fortran字符
Fortran if...else if...else 語句
Fortran調試程序
Fortran編程風格
Fortran if...then語句結構
Fortran嵌套循環(huán)
Fortran常量
Fortran循環(huán)
Fortran導出數(shù)據(jù)類型
Fortran字符串
Fortran操作函數(shù)
Fortran do...while循環(huán)結構
Fortran內部函數(shù)
Fortran數(shù)字精度
Fortran選擇決策
Fortran重塑函數(shù)
Fortran運算符
Fortran構造函數(shù)
Fortran模塊
Fortran位置函數(shù)
Fortran數(shù)字
Fortran指針
Fortran算術運算符
Fortran exit語句
Fortran動態(tài)數(shù)組
Fortran嵌套if結構
Fortran select case結構
Fortran向量和矩陣乘法函數(shù)
Fortran邏輯運算符
Fortran if...then...else 結構
Fortran教程
Fortran過程
Fortran Stop語句
Fortran基本輸入輸出
Fortran do循環(huán)結構
Fortran查詢函數(shù)

Fortran模塊

模塊就像一個包,可以包含函數(shù)和子程序,如果正在編寫一個非常大的項目,或者函數(shù)或子程序需要在多個程序中使用。

模塊提供拆分多個文件之間程序的方式。

模塊用于:

  • 包裝子程序,數(shù)據(jù)和接口塊。
  • 定義,可以使用多于一個常規(guī)全局數(shù)據(jù)。
  • 聲明可以選擇的任何程序內提供的變量。
  • 導入整個模塊,可使用在另一個程序或子程序。

模塊的語法

模塊由兩部分組成:

  • 規(guī)范的一部分,語句聲明
  • 包含一部分用于子程序和函數(shù)定義

模塊的一般形式是:

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

使用一個模塊到程序中

可以將一個程序或子程序通過使用聲明的模塊:

use name  

請注意

  • 可以根據(jù)需要添加盡可能多的模塊,在不同的文件中,并單獨編譯。

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

  • 一個模塊在同一程序中可使用多次。

  • 在模塊規(guī)格說明部分內聲明的變量,在模塊是全局的。

  • 在一個模塊中聲明的變量成為在模塊中使用的任何程序或例程的全局變量。

  • 使用聲明可以出現(xiàn)在主程序中,或任何其他子程序或模塊,它使用所述例程或在一個特定的模塊聲明的變量。

示例

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

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

當編譯并執(zhí)行上述程序,它會產(chǎn)生以下結果:

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 語句聲明。

但是,可以控制模塊代碼中使用的private 和 public 屬性的訪問性。當聲明一些變量或子程序為私有,這是不可以用在模塊之外使用。

示例

下面的例子說明了這個概念:

在前面的例子中,有兩個模塊變量,e和PI。把它們設置為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

當編譯和執(zhí)行上面的程序,它提供了以下錯誤信息:

   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

由于e 和 pi兩者都聲明為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*, 上一篇:Fortran教程下一篇:Fortran導出數(shù)據(jù)類型