Fortran/Fortran variables

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

← Beginning Fortran | Fortran simple input and output →

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.

Examples of variables:

 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)

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