FORTRAN 77 requires only a limited character set. For truly portable software, you should use only the following:
Character(s) | Description |
---|---|
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z |
26 upper-case letters |
0 1 2 3 4 5 6 7 8 9 |
10 digits |
+ |
plus |
- |
minus |
* |
asterisk |
/ |
(forward) slash |
( ) |
left and right parentheses |
= |
equals |
. |
full stop or period |
, |
comma |
' |
apostrophe |
: |
colon |
$ |
currency symbol |
|
blank |
The letters A-Z and digits 0-9 are often grouped together and referred to as alpha-numeric characters. Although this character set seems somewhat limited, it should be available on any computer. The blank character is ignored except within character constants and the currency symbol is not actually used. Of course, most computer keyboards provide many other characters, not least of which are the lower-case letters. Any printable characters may be used within comments and character constants.
Symbolic names can be used to identify constants, variables, arrays, functions, subroutines and common blocks. Each symbolic name must begin with a letter and may be followed by up to five letters or digits.
Valid symbolic names include
NAME
HE3
X
FINISH
Invalid symbolic names are
12C
(begins with a digit)Y_1
(contains an underscore character)pi
(contains lower-case letters)COMPLETE
(too long)
Be careful using O
(oh) and 0
(zero) in symbolic
names as they are easily confused. Another troublesome twosome is
I
(eye) and 1
(one), not to mention
S
(ess) and 5
(five). It is not illegal to use
these characters in symbolic names but you must take extra care not to mix
them up.
A leftover from the bad old days of punch cards is the statement layout in FORTRAN 77. Each line is divided into four fields:
A statement label is a number from 1 to 99999 that precedes a statement. Any executable statement (and some nonexecutable ones) may have a label.
The continuation field is used only when a statement is too long to fit on one line (within columns 7 to 72). A statement may be continued on the next line by placing any symbol (like the currency symbol) in the sixth column preceding the continuation. Up to 19 continuations are permitted for a single statement; hence, a single statement can cover up to 20 lines. Finally, the statement itself is placed within columns 7 to 72. Any characters that stray past the last allowed column will be ignored by the compiler and will almost certainly cause an error in the program.
It is imperative to document your programs. A piece of code that is
clear today will be difficult if not impossible to understand six months
down the road so you should include comments in your program explaining
what it does. To place a comment line in a program, place a C
or an asterisk *
in the first column. The text of the comment
follows. Each comment line must be preceded by a C
or an
asterisk *
. Like statements, comments should not extend past
column 72 but this rule is rarely enforced. A comment may appear anywhere
in a program except after the END
statement. Completely blank
lines are also permitted.
FORTRAN 77 has no reserved words but it is unwise to use the various
keywords like DATA
or DO
or IF
or
intrinsic function names as symbolic names. It makes for very
difficult-to-read code.
Blank spaces in columns 1 through 5 and 7 through 72 are ignored in FORTRAN 77. (The blank in column 6, the continuation field, is significant.)
The following assignments statements are equivalent in FORTRAN 77.
COUNT = 100 COUNT=100 COUNT = 1 0 0 COUN T = 10 0 C 0 U N T = 1 00
This also means the the spaces between keywords are ignored. For example,
END IF
and ENDIF
are treated exactly the same,
as is BLOCK DATA
and BLOCKDATA
,
GO TO
and GOTO
, etc. Which form you
choose to use (with a space or without) is down to personal preference.
A well-known example of this particular foible of FORTRAN occurred at
NASA
during the early days of spaceflight. During later testing of an orbit
computation program used on the Mercury flights, the answers kept coming
back slightly incorrect. Delving into the source code revealed the
following statement at the beginning of a DO
loop:
DO 10 I=1.10
The compiler, ignoring blanks, correctly interpreted this statement as
DO10I = 1.10
which is an assignment statement and clearly not what the programmer
intended. The full stop .
was changed to a comma
,
and the statement became
DO 10 I=1,10
after which the program ran correctly. This is also why FORTRAN 77
allows an optional comma between the label and the
loop-control-variable in a DO
loop.
A complete program consists of one or more program units. There is exactly
one main program which begins with a PROGRAM
statement:
PROGRAM main-name
This statement appears only at the beginning of the main program and the symbolic name main-name must be unique (not used elsewhere in any of the program units).
There may also be any number of external procedures (functions and
subroutines) and BLOCK DATA
subprograms. Every program
unit must end with an END
statement.
END
The END
statement is executable and may have a label. When an
END
statement is encountered in an external procedure or
BLOCK DATA
subprogram, control is returned to the calling
program unit. When an END
statement is encountered in the
main program, any open files are closed and execution stops. At this point
control is returned to the operating system.
FORTRAN 77 statements must appear in a certain order within a program unit.
BLOCK DATA ,
FUNCTION , PROGRAM , SUBROUTINE |
||
FORMAT |
PARAMETER |
IMPLICIT |
type declarations and other specification statements:
COMMON , DIMENSION , EQUIVALENCE ,
EXTERNAL , INTRINSIC , SAVE |
||
DATA |
statement functions | |
executable statements | ||
END |