Variables, types, and declarations
A D V E R T I S E M E N T
Variable names in Fortran consist of 1-6 characters chosen
from the letters a-z
and the digits 0-9.
The first character must be a letter. (Note: Fortran 90 allows variable names of
arbitrary length). Fortran 77 does not distinguish between upper and lower case,
in fact, it assumes all input is upper case. However, nearly all Fortran 77
compilers will accept lower case. If you should ever encounter a Fortran 77
compiler that insists on upper case it is usually easy to convert the source
code to all upper case.
Types and declarations
Every variable should be defined in a declaration.
This establishes the type of the variable. The most common declarations
integer list of variables
real list of variables
double precision list of variables
complex list of variables
logical list of variables
character list of variables
The list of variables should consist of variable names
separated by commas. Each variable should be declared exactly once. If a
variable is undeclared, Fortran 77 uses a set of implicit rules to
establish the type. This means all variables starting with the letters
i-n are integers and
all others are real. Many old Fortran 77 programs uses these implicit rules, but
you should not! The probability of errors in your program grows
dramatically if you do not consistently declare your variables.
Integers and floating point variables
Fortran 77 has only one type for
integer variables. Integers are usually stored as 32 bits (4 bytes) variables.
Therefore, all integer variables should take on values in the range [-m,m] where
m is approximately 2*10^9.
Fortran 77 has two different
types for floating point variables, called
double precision. While
real is often adequate, some numerical calculations
need very high precision and
double precision should be used. Usually a real is a 4 byte variable and
the double precision is 8 bytes, but this is machine dependent. Some
non-standard Fortran versions use the syntax
real*8 to denote 8
byte floating point variables.
Some constants appear many times
in a program. It is then often desirable to define them only once, in the
beginning of the program. This is what the
parameter statement is for. It also makes programs
more readable. For example, the circle area program should have been written
real r, area, pi
parameter (pi = 3.14159)
c This program reads a real number r and prints
c the area of a circle with radius r.
write (*,*) 'Give radius r:'
read (*,*) r
area = pi*r*r
write (*,*) 'Area = ', area
The syntax of the parameter statement is
parameter (name = constant, ... , name =
The rules for the
parameter statement are:
defined in the statement is not a variable but rather a constant
whose value can never change
can appear in at most one parameter statement
must come before the first executable statement
Some good reasons to use the
parameter statement are:
it helps reduce the number of
it is easy to change a constant
that appears many times in a program