Chapter 1
Essentials of Fortran-90/95/2003
Background and Introduction
[Return to Math 60 Homepage |
Return to Brief Contents Page]
[Return to Full Contents Page]
Contents
FORTRAN is an acronym for FORmula TRANslation. This scientific
computer language was developed in the early 1950's by John W.
Backus of the International Business Machine Corporation (IBM) in
San Jose, California as an easier-to-use alternate to
coding programs in what is now called "Assembly Language." It was
designed as a relatively simple language that could translate
algebraic formulas such as, a = b + c / d into computer
code, such as,
A = B + C / D
Since its original definition in 1955, Fortran has gone through a
number of revisions, and today there are several versions of
Fortran in use.
The combination of early simple rules (which are, in general, still
in effect) with newer features gives a certain simplicity to
basic Fortran programs. For example, the following C++
code
#include <iostream.h>
int main ()
{
cout << "Hello, world!" << endl;
return 0;
}
could be rendered in the latest version of Fortran as:
program test1
write(*,*) "Hello, world!"
end
The major dialects currently in use are called FORTRAN-77 and
Fortran-90/95. An earlier version, called FORTRAN IV (or
FORTRAN-66), appeared in 1966 followed by FORTRAN-77 in 1977. In
1992 a new version of Fortran was approved incorporating many new
features, and this version was called Fortran-90. Certain minor
changes were agreed upon in 1995 resulting in Fortran-95. The
last revision was published in 2003 and is commonly
called "Fortran-2003." It may, however, take a few years until
Fortran 2003 features will become readily available in compilers.
Since the differences between
Fortran-90 and Fortran-95 are so few and minor, these versions
are often referred to by the one name of "Fortran-90/95."
Fortran-2003 has added several new features, noted later,
but has not substantially altered the basic revised version
introduced as Fortran-90. For that reason, the text will
usually refer to the current version of Fortran as "Fortran-90/95/2003,"
except when specifically referring to the older version of the
language or to new features introduced with Fortran-2003.
One minor change introduced with Fortran-90 is to use the standard
capitalization rule when writing the name "Fortran."
(Formerly the language name was written with all upper case letters
because the name originated as an acronym. It is no
longer considered necessary to continue the earlier practice.)
The policy used, thus far, when revising Fortran is this: New versions
have been developed that provide greater capability and more features, yet
will still run programs written in older versions of Fortran with
little or no revisions. This rule still generally holds, since
only a few features from FORTRAN-77 have been eliminated in Fortran-90/95/2003. But
certain structures have now been marked for planned
"obsolescence" and possible elimination in the future (cf.
Chapter 19).
Fortran-90/95/2003 is commonly accepted as "standard" Fortran in
Europe, but is seen as one of two options in the U.S. (the other
being FORTRAN-77). There is yet another version of Fortran,
called "High Performance Fortran" or HPF, which is a dialect
used on parallel computers (cf. Chapter 19).
Since Fortran has been so widely used for over 50 years,
contemporary programmers should know both old and new dialects to
be able to read older code as well as make use of the latest
features.
There are currently two different styles used for writing
Fortran source (program) code. A general rule prohibits the
use of both styles within the same source code file.
Different styles may be used in the same program, however, if
subprograms of one source form are placed together in one file and
compiled independently of subprograms written in the other source form.
- One style is called Fixed Source Format and it is the
older style in use before Fortran-90/95/2003. It is based on the
limitations of the 80 column punch card (the "Hollerith" card).
In this format, each column in each line is important!!!
- The other style is called Free Source Format and is
the newer style introduced with Fortran-90 and commonly used in
other contemporary languages. In this style, there is no
limitation as to where code can be placed on a line.
On many Unix systems, the distinction between styles is indicated
by the file extension in this way:
-
If a file has the extension .for or .f it is
assumed to consist of code written in the older fixed source
format.
-
If a file has the extension .f90 or .f95, it is
assumed to consist of code written in the newer free source format.
When a Fortran-90/95/2003 compiler is available and one is using the
newer features of Fortran-90/95/2003, it is best to make use
of the newer free source format when entering code into a
program file. As a general practice, a programmer should try to
keep to one style when writing Fortran-90/95/2003 code, and the newer
style is preferred.
Also note that most Fortran compilers are not sensitive
to the case of letters of the alphabet. Both upper case and
lower case letters may be used and no distinction is made
between them. Thus, for example, A and a are
considered to be the same letter. Because the card punch
machines of the 1950's did not have lower case letters,
descriptions and examples in books describing older versions of
Fortran are traditionally in UPPER CASE LETTERS ONLY. Books on
Fortran-90/95/2003 will usually use a combination of UPPER and lower case
letters.
Even though many examples given in this text will use UPPER
CASE LETTERS, a programmer should remember that, for most
compilers, lower case letters may also be used.
Fortran-90 introduced the option to use "free source form"
in program code, a form similar to that used in some other
languages. In this form, statements can be put anywhere on a
line and more than one statement may be included on one line.
"Free source form" is governed by the following norms:
- A statement may begin at column 1 or anywhere thereafter
and continue to column 132.
- Statement labels, if used, may appear anywhere before the
statement or structure they label.
- Several statements may appear on a single line. If this
occurs, the statements are
separated by semi-colons. (This is also permitted
in the fixed source form of Fortran-90/95/2003, but not in earlier
versions of FORTRAN.)
- A new line separates one statement from another. Thus,
unlike in Pascal or C/C++, a semi-colon is not needed at
the end of each line. (Note that statements are therefore not
automatically continued from one line to the next without a
continuation sign.)
- To indicate that a statement continues from one line to the
next line, an ampersand (&) is placed at the end of
the line to be continued. (There is a different way of
indicating that one line continues to the next when using "fixed source
form," described below.) One may also include another ampersand as the
first character on the next line. (Including both ampersands,
the first in column 73 of the first line, and the other in
column 6 of the second line, enables the same code to be run in both
fixed source form and free source form.) In Fortran-90/95 up to
39 continuation lines are permitted in fixed source form files.
In Fortran-2003, up to 255 continuation lines are allowed.
- A comment is begun by an exclamation point
(in column 1 or elsewhere) and continues to the end of that line.
A comment may be included on any line, after program
code. Comments exist only for one line. If the text of a
comment must continue over more than one line, the comments on
each line must be begun anew with a separate exclamation point.
- The last line of each program segment must be an END
statement which includes the word END optionally
followed by the type of the segment and the segment name (e.g.,
END PROGRAM TEST1).
- Blanks are significant and are not ignored and
so cannot be used within identifiers. (This is in contrast to
the rule for fixed source form, where all blanks, except those
in literal strings, are ignored.)
New versions of Fortran have been so written, that older programs
do not (in general) have to be revised and can still be run
on new compilers. All versions of Fortran
may thus follow rules established in the early 1950's when all
input to computers was via Hollerith (so-called "IBM" or
"computer punch") cards. This older style for how program
code is arranged on lines is now termed "fixed source form."
Note that Fortran-90/95 structures and features may be used
even when writing code in the older fixed source form!
The following rules apply to program code written in "fixed source
form."
- There are four types of "lines" (or "cards" if one
thinks in terms of entering information via computer cards).
- a) Initial (Statement) lines
- b) Continuation lines
- c) Comment lines
- d) END lines
("Cards" and "lines" will be used interchangeably hereafter.)
- Each line has 80 columns and lines not used for comments
are subdivided into four subsections or "fields."
- a) Columns 1-5 forms the first field and these columns
are reserved for the numeric statement label
(vaguely corresponding to the line number in old versions of the
BASIC programming language, although each Fortran line does NOT
have to have a label). For most lines, this field is left blank.
If a statement label is needed, any integer number (from 1 to 5
digits) may be placed anywhere in the field--no distinction is
made between right or left justification.
- b) Column 6 is used to indicate whether a line is a
continuation of the previous line. For most lines,
this field is also left blank. Any non-blank
character in column 6 will turn a line into such a
"continuation line." (Often, programmers use the numbers 1, 2,
etc., in this column to indicate multiple continuation lines.)
In fixed source form, one may have up to 19 consecutive
continuation lines. (More lines are permitted in Fortran-90/95/2003
free source form, see previous section.)
- c) Columns 7-72 are used for any valid Fortran statement.
Except within literal quotation strings, the compiler ignores
all blanks in this field when it interprets the information.
(Blanks do take up one of the allowable columns, however.)
Although fixed source form Fortran statements cannot
start before column 7, it is permitted to start such
statements anywhere AFTER column 7. Contemporary guidelines
for programming style recommend that the interiors of loops or
IF statements always be intended for readability, and
these guidelines also apply to fixed source form Fortran code!
(Note that it was not the style to indent subsections of code
in the 1950s and 1960s and one often sees older code without any
indentations, a style not to be imitated.)
- d) Columns 73-80 were once reserved for punching a machine
generated line identification number (or sometimes a programmer
chosen job number). Since the number in this field was an
identification number rather than a programming instruction, even
in recent versions of Fortran, ANY information which a
programmer might put in this field is ignored! Thus, even
though something may appear on a line past column 73, it is
ignored by the compiler (and thus can be a source of errors)!
Every character is ignored even if what appears after column 73 are the last
few letters of an identifier (i.e., variable).
- If the letter C appears in column 1, the entire line is
considered to be a COMMENT, and is ignored by the compiler.
FORTRAN-77 also allows an asterisk, *, in column 1 instead of a
C. A comment line cannot come between an initial line and any
continuation lines. A Fortran comment exists only for one line,
and cannot be continued to the next line. If the text of a
comment must continue over more than one line, each line must be
designated as a separate comment line by the use of another C
(or *) in column 1.
- Fortran-90/95/2003 permits the use of the exclamation point, !, as
an alternative starting symbol for a comment line. Fortran-90/95 also
permits starting a comment anywhere in the line in fixed source
form when beginning the comment with an exclamation point.
- Each section of code (i.e. main program, subroutine,
function), must have an END statement as the last line
physically. An END statement is simply a line which with
the word END somewhere in columns 7-72 and blank spaces
elsewhere. (Fortran-90/95 free source code also requires an
END statement as the last line physically, but this is
no longer classified as a distinctively different type of line.)
- A line which is neither a continuation line, a comment line,
or an END line is called an INITIAL line and can contain any
valid Fortran statement (subject to the column restrictions mentioned
above).
- Older versions of Fortran only allowed one statement per
line, but Fortran-90/95 and some versions of FORTRAN-77
permit more than one statement per line using the semi-colon, ;,
as a separator.
- Some compilers will give an error or warning message if a
completely blank line is encountered. Thus, if blank lines are
desired to improve readability of the source code, such lines
should have a C (or * or !) in column 1 to avoid any unnecessary
error or warning messages.
- As noted above, blanks are ignored in statements when not
included in a literal string. One result is that they can be included within
identifiers. Even though blanks are ignored, the position they
hold within a line is counted and is used to determine the column
72 limit for statement recognition.
The PROGRAM header statement is an optional initial
statement for the main program.
Its sole purpose is to associate a name with the main program
segment and, from the perspective of programming style, its
use is highly recommended.
(The PROGRAM statement
was introduced in FORTRAN-77 and its optional character is
retained in Fortran-90/95 for the sake of compatibility with
earlier Fortran code. Most earlier dialects of Fortran did not
recognize the statement.)
If used, its form is:
PROGRAM name
After the keyword PROGRAM one can put any sequence of
letters and numbers (beginning with a letter) forming an identifier
to be used as the "name" of the main program.
Compared to other popular programming languages, the Fortran program
skeleton is almost non-existent. Because there are no generally
"reserved words" in Fortran, the skeleton is merely:
PROGRAM name {optional}
[variable type declarations] {optional}
<statements>
END
(As noted in 1.3, note 7, the last line could also be END PROGRAM
or END PROGRAM name.)
In FORTRAN-77, the last executable statement before the
END is usually a STOP statement. Its format is
merely
STOP
Fortran-90/95 allow the omission of a STOP if it would come right
before the END line.
The STOP statement terminates the program, normally
returning control back to the operating system. (Thus, its
function is similar to that of a return statement in a
C/C++ main() program segment or the exit
statement in any C/C++ function.)
Because of the existence
of Fortran statements such as IF ... ELSE and various
loops (as well as the discouraged "GO TO" statement), the
STOP statement (which marks the logical ending
point of a program), need not always come right before the
END line (which marks the physical ending point
of a program segment). It should also be noted that more than one
STOP may be included in a program.
To help distinguish between multiple STOP statements, an
optional access code may follow the word STOP. This
code is either a character string (enclosed by single or double quotes)
or a numeric string of up to five digits, for example:
STOP "Point 1"
STOP 3245
This access code will appear (sometimes preceded by word STOP,
depending on the operating system) when
the programs stops and control is returned to the operating system.
A STOP statement can be useful when a program code needs
an emergency exit in case of data or computational error.
The following is a sample Fortran-90/95 code written using the
"free source form." The column numbers and line
numbers are only for ease of reference and are never inserted in
an actual program file.
Column numbers
00000000011111111112222222222333333333344444444445
12345678901234567890123456789012345678901234567890
Line numbers
1 PROGRAM SAMP1
2 !
3 ! SAMPLE PROGRAM TO PRODUCE A LIST OF
4 ! NUMBERS AND THEIR SQUARES
5 !
6 ! VARIABLE DECLARATIONS
7 !
8 INTEGER NUM,NUMSQR
9 !
10 WRITE(6,*) 'Number - Number-Squared'
11 DO NUM=1,10
12 NUMSQR = NUM*NUM
13 WRITE(6,101) NUM,NUMSQR
14 101 FORMAT(1X,I5,I10)
15 END DO
16 STOP
17 END
COMMENTS
Lines 2-7, and 9 are comment lines. Note that lines 2, 3, 7 and 9
are blank comment lines, inserted for readability.
Lines 11-15 form the calculation loop, which is performed 10
times. The operation of this loop is discussed in
Chapter 6.
For readability, the statement internal to the loop have been
indented.
Lines 10 and 13 are WRITE statements, used to output
information from the program. The WRITE statement
is discussed in Chapters
3 and 4.
Note that line 14 contains a statement number before the keyword
FORMAT. This statement number is referenced back in
line 13 (the second number after the keyword WRITE).
Note also that none of the 17 lines in the program is a
"continuation" line since in each case, there is no ampersand as
the concluding symbol on the preceding line.
Line 16, the STOP statement, is the logical end of the program.
After the completion of the loop, there is nothing more to do, so
the computer is told to STOP. Since it occurs
immediately before the END statement, the STOP
statement, in this case, may be omitted.
Line 17, the END statement, is the physical end of this segment
of the program. In longer programs, code for subprograms
(such as a SUBROUTINE or a FUNCTION)
are included at this point, after the main program.
The following is a sample Fortran-90/95 code using the older
"fixed source form." The column numbers and line
numbers are only for ease of reference and are never inserted in
an actual program file. Remember that statements normally
start no earlier than column 7 in this form!
Column numbers
00000000011111111112222222222333333333344444444445
12345678901234567890123456789012345678901234567890
Line number
1 PROGRAM SAMP1
2 C
3 C SAMPLE PROGRAM TO PRODUCE A LIST OF
4 C NUMBERS AND THEIR SQUARES
5 C
6 C VARIABLE DECLARATIONS
7 C
8 INTEGER NUM,NUMSQR
9 C
10 WRITE(6,*) 'Number - Number-Squared'
11 DO NUM=1,10
12 NUMSQR = NUM*NUM
13 WRITE(6,101) NUM,NUMSQR
14 101 FORMAT(1X,I5,I10)
15 END DO
16 STOP
17 END
COMMENTS
The comments for this section are very similar to the comments
for the previous section. One major difference is starting the
statement portion of each line in column 7 (or later). Other
major differences are noted here.
The comment lines in this example begin with a C in
column 1 of each line. In FORTRAN-77, the * could be used in
place of a C and in Fortran-90/95, a ! can be used
instead.
In the calculation loop in lines 11-15, note that while line 11
and 15 start in column 7, the interior of the loop is indented
for readability.
Note that line 14 contains a statement number in the initial
field (columns 1-5).
Note also that none of the 17 lines in the program is a
"continuation" line since, in each case, column 6 is left blank.
This section lists the major features of Fortran, which may be
useful for those who have programmed in other languages.
Major Positive Points:
- PORTABLE CODE: Because of the standardization of Fortran,
programs can be readily moved from one compiler to another, and
usually run with only minor changes, if any.
- EFFICIENT COMPILERS: Fortran compilers are very well
developed and efficient, often with many optimization features, and are
available for almost all machines, from personal computers to
parallel computers. Because of compiler efficiency, Fortran programs
run very fast and usually out-perform compilers for most other
languages.
- EXPONENTIATION OPERATOR: Unlike many other languages,
Fortran has an exponentiation
operator, **, which makes it particularly useful for solving
numerical problems.
- MATHEMATICAL FEATURES: Standard Fortran contains several
features which make it very appropriate for mathematical
programs: complex and multiple-precision real data types, the
exponentiation operator, and a large library of built-in mathematical
functions.
- POWERFUL INPUT/OUTPUT: Fortran has a developed input/output
system with many features. As a result, the input and output
system is very powerful (but can also be very confusing to use).
Points of Difference between Versions of Fortran and with Other
Languages
- BLANKS ARE IGNORED IN FIXED SOURCE FORM: As noted earlier,
all blanks (except in literal quoted strings) are ignored
when using Fixed Source Form. Thus this style of source code
(used in FORTRAN-77 and other earlier versions) differs from some
other languages, where certain blanks are needed, and only "extra"
blanks and carriage returns are ignored. In fixed source form,
blanks are even ignored as part of variable names (see
Chapter 2).
- IN FORTRAN-77, IDENTIFIERS MAY BE SIX CHARACTERS LONG AT MAXIMUM:
In FORTRAN-77 (and other earlier versions of FORTRAN), variables and
subprogram names could (normally) be at most six characters long.
Therefore, a programmer had to be somewhat creative in
choosing appropriate identifiers. In Fortran-90/95, this limitation
has been extended.
- DEFAULT VARIABLE TYPING: Variables do not have to be declared
in Fortran. Variables that are not explicitly declared (some due to
a typographical error) are given a default numeric `type' (either integer or
real) based on the first letter (cf.
section 2.2). One can turn
"off" default typing by including the statement IMPLICIT NONE before
any variables are declared.
- NO RECURSION PRIOR TO FORTRAN-90/95: FORTRAN-77 subprograms are
not recursive, but Fortran-90/95 has introduced recursion for subprograms.
- ALL VARIABLES ARE LOCAL: All variables in Fortran are local to
the subsection in which they are used and/or declared. There are
no global variables in Fortran. On the other hand, the old FORTRAN
COMMON statement and the Fortran-90/95 MODULE construction
are ways to designate certain variable as being able to be shared by
multiple segments of a program.
- STATEMENT NUMBERS ARE LOCAL: All
statement numbers (if used) are local. Numbers used in one
subsection are independent of any used in another subsection.
- NO EXPLICIT DESIGNATION OF BLOCKS: Unlike C/C++ or other languages,
Fortran needs no special set of delimiters to indicate a block
of statements, e.g., the { ... } pair in C/C++ (or
the begin ... end pair in Pascal). Most Fortran-90/95 statements or
constructs assume that there will be more than one statement in
the body of the construct. Thus only a labeled END (e.g., END IF
or END DO) is used to indicate the end of the construct.
Often, there is only one END per section--physically
the last line of the section, typically unlabeled.
- SELECTED USE OF SEMI-COLONS: Standard Fortran does not use
semi-colons to end or separate statements appearing on
different lines. (Prior to FORTRAN-77, only one statement
could appear on a line.) If the programmer desires to put more
than one statement on a line, the statements are separated by
semicolons.
- NEW LINES HAVE MEANING: Because semi-colons are not part of
the normal Fortran syntax, the new line (carriage return) usually
is the statement separator. If one line continues information
from the previous line (e.g., a very long arithmetic expression),
it must be indicated as a continuation line.
As noted
above, this is done in Fixed Source Form by using a non-blank
character in column 6 of the line that continues
the previous line, or, in Free Source Form, by using an
ampersand as the last character of the line to be continued.
- SPAGHETTI-CODE SYNDROME: Old versions of FORTRAN could lead
to a so-called "spaghetti-code" style of programming, in which
later sections of the code could jump to earlier sections and
earlier sections to later sections. This syndrome could be avoided, however,
if the programmer used disciplined, structured techniques. The
"spaghetti-code" style means that older FORTRAN
programs are often much harder to read than programs written in
FORTRAN-77, Fortran-90/95 or other "structured" languages.
This page is maintained by Dennis C. Smolarski,
S.J. dsmolarski@math.scu.edu
© Copyright 1999-2006 Dennis C. Smolarski, S.J., All rights
reserved.
Last changed: 5 January 2006.