5. Variables, types, and declarations
Variable names
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. Fortran 77 does not distinguish between upper and lower case, and
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.
The words which make up the Fortran language are called reserved
words and cannot be used as names of variables. Some of the reserved
words which we have seen so far are "program", "real", "stop" and "end".
Types and declarations
Every variable should be defined in a declaration. This establishes
the type of the variable. The most common declarations are:
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 used
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 bit (4 byte) 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
real and 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.
The parameter statement
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 rather have been written like
this:
program circle
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
stop
end
The syntax of the parameter statement is
parameter (name = constant, ... , name = constant)
The rules for the parameter statement are:
-
The name defined in the parameter statement is not a variable
but rather a constant. (You cannot change its value at a later point in
the program.)
-
A name can appear in at most one parameter statement.
-
The parameter statement(s) must come before the first executable
statement.
Some good reasons to use the parameter statement are:
-
It helps reduce the number of typos.
-
It makes it easier to change a constant that appears many times in a program.
-
It increases the readability of your program.
Copyright © 1995-7 by Stanford University. All rights reserved.
[ME-390 Home
Page][Fortran
77 Tutorial Home]
hargrove@sccm.Stanford.EDU