Fortran/Fortran variables

Introduction edit

In programming, a variable is a container for data that the program can change. You typically declare variables before you use them to provide information on what kind of data they should store. However, Fortran allows variables to be created implicitly. Absent an implicit statement, undeclared variables and arguments beginning with i/I through n/N (the "in" group) will be integer, and all other undeclared variables and arguments will be real.

Many consider using variables without declaring them bad practice. If you want to be forced to declare variables, code implicit none first.

General Examples edit

Examples of usual variables are listed below

! Declare a constant, whose value cannot be changed.
integer, parameter :: num_days_week = 7
! Declare i as an integer, j as an array of 2 integers from j(1) to j(2), k as
! an array of 2 integers from '''k(0)''' to k(1), and m as a 2-dimensional
! array of 12 elements.
integer :: i, j(2), k(0:1), m(3,4)
! Declare c as an array of 4 floating point numbers from c(0) to c(3).
real :: c(0:3)
! Declare word as a string of length 5
character (len=5) :: word
! Declare a boolean variable with values .TRUE. or .FALSE.
logical :: tf

The following does exactly the same thing, but in the shorter, more archaic form:

INTEGER, PARAMETER :: num_days_week = 7
DIMENSION j(2), k(0:1), m(3,4), c(0:3)

If memory layout counts to you, note that m(1,1) is followed in memory by m(2,1), and not by m(1,2).

A variable can be set by placing it before an equal sign, which is followed by the value to which it is set. Given the declarations above, the following assignments are possible:

i    = 3*4                  ! Set i to 3*4 = 12         
j    = [1, 4]               ! Set j(1) to 1, j(2) to 4
c    = [1.0, 4.0, 5.0, 9.0] ! Set c(0) to 1.0, c(1) to 4.0, c(2) to 5.0, c(3) to 9.0
word = 'dog'                ! Set word = "dog  " . The variable word is padded with spaces on the right
tf   = .true.               ! Set tf to True

A variable can appear on both sides of an assignment. The right hand side is evaluated first, and the variable is then assigned to that value:

i = 3     ! i has value 3
i = i**i  ! i has value 3**3 = 27

Variables can be converted from one type to another, but unlike in C++ or Java where you would typecast the variable, in Fortran you use the intrinsic procedures:

real          :: r = 1.5
real (kind=8) :: d = 1.5
integer       :: i = 1

print *, dble(r), dble(d), dble(i)   ! Convert number to a double precision
print *, real(r), real(d), real(i)   ! Convert number to a single precision (REAL)
print *, int(r), int(d), int(i)      ! Convert number to an integer

Again, the same thing in the simpler, archaic form:

 r = 1.5
 i = 1
 PRINT *, DBLE(r), DBLE(d), DBLE(i)
 PRINT *, REAL(r), REAL(d), REAL(i)
 PRINT *, INT(r), INT(d), INT(i)

Arrays edit

Declaration edit

One can declare arrays using two different notations. The following example illustrates the notations for arrays of integer type and of length 5.

integer, dimension (5) :: arr1
integer                :: arr2(5)

For multidimensional arrays one needs to specify the length of each dimension. The following example highlights the case of a 5x6 integer matrix aka a two-dimensional array of length (5,6). (Again, showing both notations.)

integer, dimension (5,6) :: arr1
integer                  :: arr2(5,6)

Initialization edit

To initialize arrays with actual values one has multiple options: set specific elements, specific ranges, or the whole array.

integer :: arr(3)

arr(1)   = 4            ! set specific element
arr(1:2) = [4, 5]       ! set a range aka slicing notation
arr      = [4, 5, 6]    ! set whole array

To set multidimensional arrays one need to make use of reshape, and shape commands.

integer :: arr(2,3)

arr = reshape([1,2,3,4,5,6], shape(arr))
! arr = reshape([1,2,3,4,5,6], shape=[2,1])  ! same effect as above command - hardcode the shape of arr

! arr represents matrix:
! 1 3 5
! 2 4 6

Fortran uses column-major ordering such that the upper example produces a often confusing matrix. For a row-major ordering one can use the following example which highlights the use of the order argument to specify along which dimension to sort first.

integer :: arr(2,3)

arr = reshape([1,2,3,4,5,6], shape(arr), order=[2,1])

! arr represents matrix:
! 1 2 3
! 4 5 6