CSE 505 Lecture Notes:
Fortran
October 10, 1994
Introduction
Fortran stands for "FORmula TRANslating system".
First proposal: 1954 (at IBM) by John Backus. Work continued for the next
two years; initial release in 1957. At that time, there was much
skepticism about higher-level languages -- Fortran had to be efficient to
be accepted.
These notes primarily concern Fortran IV.
Control structures
Control structures are based on IBM 704 branch instructions (Backus
initially thought of Fortran as a language for the 704; there would be
somewhat different languages for different machines)
Fortran has an arithmetic if statement ...
IF (EXPR) N1, N2, N3
... which is not that useful (two-way branches more common than three-way).
Later versions of Fortran had a logical if:
IF (A .LT. 5) A=A-1
There could only be a simple statement in the consequent of the IF (but not
a compound statement, such as another IF or a DO loop).
Additionally, Fortran has the GOTO statement:
- unconditional:
GOTO 100
- computed GOTO:
GOTO (30,40,50),K
(30, 40, and 50 are statement numbers)
Note that we can build other control structures from GOTO's:
- while loop
- repeat-until loop
- for loop
Drawbacks of the GOTO:
- not as clear as structured control statements
- harder for compiler to produce good code
- spaghetti-style programming
See Dijkstra's famous letter to the editor: E.W. Dijkstra, "GOTO Statment
Considered Harmful", CACM, 1968 for more on the GOTO controversy.
There have been countless papers and letters since of the form "X
Considered Y" in imitation, e.g.
Rick Hehner, "DO Considered OD" (about Dijkstra's DO .... OD control
structure)
Structured programming
The Structure Principle:
The static structure of a program should correspond in a simple way to the
dynamic structure of the corresponding computation.
Control structures such as WHILE, FOR, and IF-THEN-ELSE support the
structure principle; GOTO's do not.
General Features and Problems of FORTRAN
storage layout: statically allocated (hence no recursion)
data types: fixed, built-in set
- integer, float, double precision float, complex, etc.
- arrays
optional declarations -- if there is no declaration for a variable, it
defaults to INTEGER if the first letter is I-N, otherwise REAL
This is no longer considered a good idea, since, for example, it
doesn't catch typos in variable names.
declarations local in scope to subroutines, main program
common blocks (anonymous and named) allow sharing among subroutines
common blocks provide a type loophole, since the programmer can have an
INTEGER in one subroutine allocated in the same storage location as a REAL
in another subroutine -- hence FORTRAN is
weakly typed
equivalence statements -- another type loophole
FORTRAN syntax is based on fixed format (punch card influence)
blanks ignored (a mistake)
these statements all mean the same thing:
DO 10 I=1,10
DO10I=1,10
D O1 0I=1,1 0
... but this assigns 1.1 to the variable DO10I!
DO 10 I=1.10
no reserved words (also a mistake)
IF (I) = 1 2 3
IF (I) 1,2,3
... the first statement assigns 123 to element I of the array IF, while the
second is an arithmetic IF statement
major win: algebraic expressions
(X*Y+10)/20.4
Evolution to subsequent languages:
- Algol-60
- Pascal, Modula-2, Ada, etc.
- PL/I