FORTRAN requires only a limited character set. For truly portable software, you should use only the following:
| Character(s) | Description |
|---|---|
| ABCDEFGHIJKLMNOPQRSTUVWXYZ | 26 upper-case letters |
| 0123456789 | 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
NAMEHE3XFINISHInvalid 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. 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.
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 (subroutines and
functions) and BLOCKDATA 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
BLOCKDATA 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 statements must appear in a certain order within a program unit.
BLOCKDATA, FUNCTION,
PROGRAM, SUBROUTINE |
||
FORMAT |
PARAMETER |
IMPLICIT |
type declarations and other specification statements:
COMMON, DIMENSION, EQUIVALENCE,
EXTERNAL, INTRINSIC, SAVE |
||
DATA |
statement functions | |
| executable statements | ||
END |
||