Fortran:module

声明:本博文翻译自:https://www.tutorialspoint.com/fortran/fortran_modules.htm

模块就像一个包,你可以保存你的函数和子程序,如果你在写一个非常大的程序,module将很好用。

module通常被用作
(1) 打包子程序、数据和接口块。不再写interface
(2) 定义可由多个例程使用的公共数据(也有人叫全局变量)。
(3) 声明可以在您选择的任何子程序中可用的变量。
(4) 将一个模块完全导入到另一个程序或子程序中以供使用。

1. module的基本语法
module包含两部分:
(1) 声明语句
(2) 包含subroutine或function
module name     
   [statement declarations]  
   [contains [subroutine and function definitions] ] 
end module [name]

2. 使用module
可以通过use语句将模块导入到程序或子例程中
但是注意以下几点
(1) 您可以根据需要添加任意多个模块,每个模块都将放在单独的文件中,并分别编译。
(2) 一个模块可以用在各种不同的程序中。
(3) 一个模块可以在同一个程序中多次使用。
(4) 模块规范部分中声明的变量是模块的全局变量。
(5) 模块中声明的变量在任何使用模块的程序或例程中都是全局变量。
(6) use语句可以出现在主程序中,也可以出现任何其他子c程序、函数或模块中。

示例程序如下:
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.141593
e =    2.718282
e raised to the power of 2.0 =    7.389056
Area of a circle with radius 7.0 =    153.9380

3. 模块中变量和子程序的可访问性
默认情况下,模块中的所有变量和子例程都通过use语句提供给使用模块代码的程序。
但是,可以使用私有和公共属性来控制模块代码的可访问性。当将某个变量或子例程声明为私有时,它在**模块外部**不可用。
示例代码如下:
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
在执行上述代码时,会出现如下错误
test.f90(23): error #6404: This name does not have a type, and must have an explicit type.   [E]
  ePowerx = e ** x
------------^
test.f90(24): error #6404: This name does not have a type, and must have an explicit type.   [PI]
  area = pi * radius**2
---------^
compilation aborted for test.f90 (code 1)

将上述代码进行改写:
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.141593
e =    2.718282
e raised to the power of 2.0 =    7.389056
Area of a circle with radius 7.0 =    153.9380
  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值