Fortran語言模塊

2018-12-12 14:24 更新

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

模塊為您提供多個文件之間的分裂你的程序的方式。

模塊用于:

  • 包裝子程序,數(shù)據(jù)和接口塊。
  • 限定,可以由一個以上的程序中使用的全局數(shù)據(jù)。
  • 宣稱可以選擇的任何程序內(nèi)提供的變量。
  • 導入模塊完全,使用,到另一個程序或子程序。

一個模塊的語法

一個模塊由兩部分組成:

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

模塊的一般形式是:

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

使用一個模塊到你的程序

您可以將在通過使用語句程序或子程序模塊:

use name  

請注意

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

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

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

  • 在模塊規(guī)范部分中聲明的變量是全局的模塊。

  • 一個模塊中聲明的變量成為在使用的模塊的任何程序或常規(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)生以下結(jié)果:

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   

模塊中的變量和子程序的無障礙

缺省情況下,一個模塊中的所有的變量和子程序被提供給正在使用該模塊的代碼,通過使用語句的程序。

但是,你可以控制的模塊代碼使用私人公共屬性的可訪問性。當你聲明一些變量或子程序為私有,這不是可用的模塊外。

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

在前面的例子中,我們有兩個模塊變量,e圓周率。讓我們把他們的私人和觀察輸出:

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圓周率,既被聲明為私有,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

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

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   

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號