Fortran-modules

提供:Dev Guides
移動先:案内検索

Fortran-モジュール

モジュールは、非常に大きなプログラムを作成している場合、または関数またはサブルーチンを複数のプログラムで使用できる場合に、関数とサブルーチンを保持できるパッケージのようなものです。

モジュールは、プログラムを複数のファイルに分割する方法を提供します。

モジュールはのために使用されます-

  • サブプログラム、データ、インターフェイスブロックのパッケージ化。
  • 複数のルーチンで使用できるグローバルデータの定義。
  • 選択したルーチン内で使用可能にできる変数の宣言。
  • 使用するために別のプログラムまたはサブルーチンにモジュールを完全にインポートする。

モジュールの構文

モジュールは2つの部分で構成されています-

  • ステートメント宣言の仕様部
  • サブルーチンおよび関数定義の一部を含む

モジュールの一般的な形式は-

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

プログラムへのモジュールの使用

あなたはuse文によってプログラムまたはサブルーチンにモジュールを組み込むことができます-

use name

その点に注意してください

  • 必要な数のモジュールを追加できます。各モジュールは個別のファイルに含まれ、個別にコンパイルされます。
  • モジュールはさまざまなプログラムで使用できます。
  • モジュールは、同じプログラムで何度も使用できます。
  • モジュール仕様部で宣言された変数は、モジュールに対してグローバルです。
  • モジュールで宣言された変数は、モジュールが使用されるプログラムまたはルーチンのグローバル変数になります。
  • useステートメントは、メインプログラム、または特定のモジュールで宣言されたルーチンまたは変数を使用する他のサブルーチンまたはモジュールに表示できます。

次の例は、概念を示しています-

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。*の2つのモジュール変数がありました。それらをプライベートにして、出力を観察しましょう-

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