Declaration of variables

A variable is represented by a symbolic name which is associated with a particular place in the memory of your computer. The value of the variable is the value currently stored in that location and this value can be changed by assigning a new value to the variable.

Prior to using a particular variable, you have to specify its data type or, for those of you who are more rigorous, declare the variable. It means that you assign a specific standard type to this variable, which in turn tells your computer to allocate a sufficient amount of space for storage of this data. This declaration of variables occurs after the statement program, subroutine or function (the last two subprograms will be explained later) and typically has the following form:

      integer number, val
      real result
In this example, integer and real are data types and number, val, result are symbolic names of three variables. It should be clear that the first two variables are declared to contain integral numbers, whereas the last one will store a real number. You can always assign a data type to more than one variable in one line, provided that the variables are separated by commas.

Fortran provides several standard data types which are frequently used in F77 codes. A very short compilation of the most useful ones is given below. You can see that each variable occupies a different space in the memory of your computer. For the beginning you will often use two data types, integer and real, the former of which is commonly used for counting loops and the latter contains the values used in your calculations.


symbolic name size description
logical 1 bit stores either logical 0 (.false.) or logical 1 (.true.)
byte 1 byte integral numbers from -128 to 127
integer 4 bytes integral numbers from $-2^{31}$ to $2^{31}-1$
real 4 bytes real numbers approx. between $1.2\cdot{}10^{-38}$ and $3.4\cdot{}10^{38}$
double precision 8 bytes real numbers approx. between $2.2\cdot{}10^{-308}$ and $1.8\cdot{}10^{308}$
complex 2 $\times$ 4 bytes two real numbers, real and imaginary part
double complex 2 $\times$ 8 bytes two double precision numbers, real and imaginary part
character 1 byte one alphanumeric character
character*N N bytes N alphanumeric characters

A common task is to declare an array which can store a vector (1$\times$N, N$\times$1), matrix (M$\times$N) or a higher-dimensional array (M$\times$N$\times$P$\times\ldots$). This can be done for any numerical data type by simply declaring:

      integer vect(5)
      real mat(4,10), tmat(2,5,9)
The variable vect is a vector of 5 components of type integer indexed from vect(1) to vect(5), mat represents a matrix of real numbers with 4 rows and 10 columns and tmat is a three-dimensional array in which each element is of type real. The elements of the lastly-mentioned array can be accessed accordingly, e.g. tmat(1,3,7).

Roman Gröger (2015-09-23)