Converted document

FORTRAN 90+: DECLARATION OF VARIABLES

The declaration of variables is very important, especially when using good programming practices without the use of implicit variables. In FORTRAN 77, it was assumed that all variables that began with the letters from “i” to “n” were integers, and variables that began with the rest of the alphabet were real numbers. This is known as implicit variable definition. Many older programmers will still use these rules in writing code, and that is not a problem as long as the variables are explicitly defined. This explicit definition of variables is known as the declaration of variables. This process must include every variable name. Once the variable has been defined, it cannot be changed. For example, if x is defined as a REAL number, it cannot be used as a CHARACTER later in the code.
The second line of a program should be
PROGRAM basics
          IMPLICIT NONE
          .........................
END PROGRAM basics
The IMPLICIT NONE means that all variables must be defined, and compilation errors will result if they are not defined.
The next lines in the code are used to define the variables. In simple programs, these will be defined as either INTEGER, REAL, COMPLEX, LOGICAL, or CHARACTER variables.
Integers are simply whole numbers, and they are the variable of choice for counters. Real numbers are able to include decimal values as well as whole numbers. Complex numbers include a real and imaginary part (an array of dimension 2 in the order of real, then imaginary). A logical variable is either .TRUE. or .FALSE., while a character is typically a string of letters, numbers or symbols.
These variables are defined using a type statement of the form:
<data_type> :: <variable list>
The first four types of variables are defined rather simply:
INTEGER :: i, j, k
REAL :: x, y, z
LOGICAL :: test
 
Notice that all of these are single valued variables. They can also be arrays.
CHARACTER STRINGS
Character strings are a little more difficult as they tend to be composed of more than one character. The number of characters in a string is also called the length of the string. There are two ways to denote the length of a string, “i”:
CHARACTER(LEN=i) :: string
CHARACTER(i) :: string
 
These two declarations are equivalent. If the length of a string is 1, then there are three ways to define it:
CHARACTER(LEN=1) :: string
CHARACTER(1) :: string
CHARACTER :: string
 
Multiple character strings can be declared in a single line, as illustrated by city with a length of 10, state of length 2, and zip of length 5. These can be defined using
CHARACTER :: city*10, state*2, zip*5
 
If the length of the character string is defined by a calculation or passed from another subprogram, it is said to be assumed and is denoted with an asterisk “*”:
CHARACTER(LEN=*) :: city, state, zip
 
INTEGER VERSUS REAL VARIABLES
An integer can be saved as a real number, typically with no consequences beyond a minor loss of speed. When a REAL number is saved as an INTEGER, the compiler truncates the decimals and saves only the whole part. Even more important is math with both REAL and INTEGER values.
Next Page →
Next Page →