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   

上一篇: Fortran過程 下一篇: Fortran內部函數