阅读(3045) (0)

Fortran语言模块

2016-12-12 14:24:08 更新

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

模块为您提供多个文件之间的分裂你的程序的方式。

模块用于:

  • 包装子程序,数据和接口块。
  • 限定,可以由一个以上的程序中使用的全局数据。
  • 宣称可以选择的任何程序内提供的变量。
  • 导入模块完全,使用,到另一个程序或子程序。

一个模块的语法

一个模块由两部分组成:

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

模块的一般形式是:

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   

模块中的变量和子程序的无障碍

缺省情况下,一个模块中的所有的变量和子程序被提供给正在使用该模块的代码,通过使用语句的程序。

但是,你可以控制的模块代码使用私人公共属性的可访问性。当你声明一些变量或子程序为私有,这不是可用的模块外。

下面的例子说明了这一概念:

在前面的例子中,我们有两个模块变量,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

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

   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

当你编译和执行上面的程序,它会产生以下结果:

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