Variables of any simple type are declared at the top of a Fortran program. Declaration of variables is, minimally, that their type is specified. Assignment of values to variables is simply giving the variables values via the assignment operator, "=", in the form <variable> = <value> (note that this is not a statement of equality but rather setting the value of the variable to <value>).

Multiple variables can be declared in a single statement, as long as the variable names are separated by commas. Scalar variables of the five basic Fortran types would be declared as follows:

Any declaration may have one or more attributes. The most common ones are parameter to declare a constant, and dimension to declare an array. Here are examples of using parameter to declare a constant:

Array indexes indicate position along a given dimension of the array, in the manner familiar from labelling of the elements in vectors and matrices (where in two dimensions, row number is the first index and column number the second). We declare static arrays (1D and multi-dimensional) using dimension:

Note that parentheses rather than brackets are used in declaring array bounds. Array bounds can be either literal constants or parameters. The lower bound is assumed to be 1 unless declared otherwise; thus, in the above examples, the arrays start at index 1 and end at index 100.

When a lower bound is declared, a colon (:) is used as the separator between the lower and upper bounds. Here is an example of a 2-dimensional array featuring special lower bounds:

Fortran arrays are stored in column-major order. This means that array elements are stored in adjacent memory addresses when they differ by 1 in the leading index. For example, if the array a is declared as follows:

...then the elements of a are stored in this order:

Arrays will be explained in detail in a later section.

Finally, let's look at examples of assignment statements using some of the variables we have declared above. Note that multiple commands on the same line are allowed, as long as they are separated by a semicolon (;).

 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement