目录

Fortran - 模块( Modules)

模块就像一个包,你可以保存你的函数和子程序,以防你编写一个非常大的程序,或者你的函数或子程序可以在多个程序中使用。

模块为您提供了一种在多个文件之间拆分程序的方法。

模块用于 -

  • 打包子程序,数据和接口块。

  • 定义可由多个例程使用的全局数据。

  • 声明可以在您选择的任何例程中使用的变量。

  • 将模块完全导入到另一个程序或子程序中以供使用。

模块的语法

一个模块由两部分组成 -

  • 声明声明的规范部分
  • a包含子程序和函数定义的部分

模块的一般形式是 -

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

将模块用于您的程序

您可以通过use语句将模块合并到程序或子例程中 -

use name  

请注意

  • 您可以根据需要添加任意数量的模块,每个模块将位于单独的文件中并单独编译。

  • 模块可用于各种不同的程序。

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

  • 在模块规范部分中声明的变量对模块是全局的。

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

  • use语句可以出现在主程序或使用特定模块中声明的例程或变量的任何其他子例程或模块中。

例子 (Example)

以下示例演示了该概念 -

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属性控制模块代码的可访问性。 当您将某个变量或子例程声明为私有时,它在模块外部不可用。

例子 (Example)

以下示例说明了这一概念 -

在前面的示例中,我们有两个模块变量epi. 让我们将它们设为私有并观察输出 -

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,都被声明为私有,因此程序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   
↑回到顶部↑
WIKI教程 @2018