変数#
変数は、プログラムによって操作できる情報を格納します。Fortranは*強い型付け*の言語です。つまり、各変数は型を持つ必要があります。
Fortranには5つの組み込みデータ型があります
integer
– 正または負の整数を表すデータ用real
– 浮動小数点データ(整数ではない)用complex
– 実数部と虚数部からなるペアcharacter
– テキストデータ用logical
– ブール値(真または偽)を表すデータ用
変数を使用する前に、*宣言*する必要があります。これにより、コンパイラに変数の型とその他の変数属性が通知されます。
Fortranは*静的型付け*の言語です。つまり、各変数の型はプログラムのコンパイル時に固定され、プログラムの実行中に変更することはできません。
変数の宣言#
変数を宣言するための構文は次のとおりです。
<variable_type> :: <variable_name>
ここで、<variable_type>
は上記の組み込み変数型のいずれかであり、<variable_name>
は変数に付けたい名前です。
変数名は文字で始まり、文字、数字、アンダースコアで構成できます。次の例では、各組み込み型に対して変数を宣言しています。
例:変数宣言
program variables
implicit none
integer :: amount
real :: pi
complex :: frequency
character :: initial
logical :: isOkay
end program variables
Fortranコードは**大文字と小文字を区別しません**。変数名の大文字小文字について心配する必要はありませんが、一貫性を保つことをお勧めします。
プログラムの先頭にある追加のステートメントimplicit none
に注意してください。このステートメントは、すべての変数が明示的に宣言されることをコンパイラに指示します。このステートメントがないと、変数は始まる文字に従って暗黙的に型付けされます。
常に各プログラムとプロシージャの先頭に
implicit none
ステートメントを使用してください。暗黙の型付けは、情報を隠蔽してプログラムエラーを増やすため、現代のプログラミングでは悪い習慣と見なされています。
変数を宣言したら、代入演算子=
を使用して、値を代入したり、再代入したりできます。
**例:**変数への代入
amount = 10
pi = 3.1415927
frequency = (1.0, -0.5)
initial = 'A'
isOkay = .false.
文字は、単一引用符('
)または二重引用符("
)で囲みます。
論理値またはブール値は、.true.
または.false.
のいずれかになります。
重要
宣言時の代入に注意してください
integer :: amount = 1
**これは通常の初期化ではありません。** save
属性が暗示されます。これは、変数がプロシージャ呼び出し間で値を保持することを意味します。良い習慣は、変数を宣言とは別に初期化することです。
標準入出力#
*Hello World*の例では、コマンドウィンドウにテキストを出力しました。これは一般に、標準出力
またはstdout
への書き込みと呼ばれます。
前に紹介したprint
ステートメントを使用して、変数の値をstdout
に出力できます。
print *, 'The value of amount (integer) is: ', amount
print *, 'The value of pi (real) is: ', pi
print *, 'The value of frequency (complex) is: ', frequency
print *, 'The value of initial (character) is: ', initial
print *, 'The value of isOkay (logical) is: ', isOkay
同様に、read
ステートメントを使用して、コマンドウィンドウから値を読み取ることができます。
program read_value
implicit none
integer :: age
print *, 'Please enter your age: '
read(*,*) age
print *, 'Your age is: ', age
end program read_value
この入力ソースは、一般に標準入力
またはstdin
と呼ばれます。
式#
一般的な算術演算子のセットが使用できます。優先順位の高い順にリストされています。
演算子 |
説明 |
---|---|
|
** べき乗 |
|
* 乗算 |
|
/ 除算 |
|
+ 加算 |
|
- 減算 |
例
program arithmetic
implicit none
real :: pi
real :: radius
real :: height
real :: area
real :: volume
pi = 3.1415927
print *, 'Enter cylinder base radius:'
read(*,*) radius
print *, 'Enter cylinder height:'
read(*,*) height
area = pi * radius**2
volume = area * height
print *, 'Cylinder radius is: ', radius
print *, 'Cylinder height is: ', height
print *, 'Cylinder base area is: ', area
print *, 'Cylinder volume is: ', volume
end program arithmetic
浮動小数点精度#
必要な浮動小数点精度は、kind
パラメータを使用して明示的に宣言できます。iso_fortran_env
組込みモジュールは、一般的な32ビットおよび64ビット浮動小数点型にkind
パラメータを提供します。
例:明示的な実数kind
program float
use, intrinsic :: iso_fortran_env, only: sp=>real32, dp=>real64
implicit none
real(sp) :: float32
real(dp) :: float64
float32 = 1.0_sp ! Explicit suffix for literal constants
float64 = 1.0_dp
end program float
浮動小数点リテラル定数には、常に
kind
サフィックスを使用してください.
例: C言語と相互運用可能なkind
program float
use, intrinsic :: iso_c_binding, only: sp=>c_float, dp=>c_double
implicit none
real(sp) :: float32
real(dp) :: float64
end program float
次のパートでは、変数に複数の値を格納するために配列を使用する方法を学習します。
block
構文を使用したローカルスコープ変数#
2008 Fortran標準では、プログラムまたはプロシージャ内でローカルスコープ変数を使用できるblock
の概念が導入されました。
例
module your_module
implicit none
integer :: n = 2
end module
program main
implicit none
real :: x
block
use your_module, only: n ! you can import modules within blocks
real :: y ! local scope variable
y = 2.0
x = y ** n
print *, y
end block
! print *, y ! this is not allowed as y only exists during the block's scope
print *, x ! prints 4.00000000
end program