Fortran/Fortran variables

From Wikibooks, open books for an open world
Jump to navigation Jump to search

← Beginning Fortran | Fortran simple input and output →


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 through 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

 INTEGER, PARAMETER :: num_days_week = 7 ! declare a constant, whose value cannot be changed
 INTEGER            :: i, j(2), k(0:1), m(3,4) ! 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
 REAL               :: c(0:3) ! declare c as an array of 4 floating point numbers from c(0) to c(3)
 CHARACTER (LEN=5)  :: word   ! declare word as a string of length 5
 LOGICAL            :: tf     ! declare a boolean variable with values .TRUE. or .FALSE.

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)
 CHARACTER*5 word
 LOGICAL tf

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
 DOUBLE PRECISION :: 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:

 DOUBLE PRECISION d = 1.5
 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