位置:首页 > > Fortran模块

Fortran模块

模块就像一个包,可以包含函数和子程序,如果正在编写一个非常大的项目,或者函数或子程序需要在多个程序中使用。

模块提供拆分多个文件之间程序的方式。

模块用于:

  • 包装子程序,数据和接口块。
  • 定义,可以使用多于一个常规全局数据。
  • 声明可以选择的任何程序内提供的变量。
  • 导入整个模块,可使用在另一个程序或子程序。

模块的语法

模块由两部分组成:

  • 规范的一部分,语句声明
  • 包含一部分用于子程序和函数定义

模块的一般形式是:

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

使用一个模块到程序中

可以将一个程序或子程序通过使用声明的模块:

use name 

请注意

  • 可以根据需要添加尽可能多的模块,在不同的文件中,并单独编译。

  • 一个模块可以在各种不同的程序中使用。

  • 一个模块在同一程序中可使用多次。

  • 在模块规格说明部分内声明的变量,在模块是全局的。

  • 在一个模块中声明的变量成为在模块中使用的任何程序或例程的全局变量。

  • 使用声明可以出现在主程序中,或任何其他子程序或模块,它使用所述例程或在一个特定的模块声明的变量。

示例

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

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 语句声明。

但是,可以控制模块代码中使用的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

当编译和执行上面的程序,它提供了以下错误信息:

   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*, "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