Basic Program Structure

Source format

Fortran's heritage from old (and plain outdated) computer systems is clearly visible in the rules for the source code format. Back in the old days, source code was kept on punched cards. Each card corresponds to one statement in the program.

Since a punched card has 80 columns, lines in a Fortran program are limited to 80 characters, and in fact, some columns have special meanings.

Because of Fortran's one-line-one-statement paradigm, there is no semcolon or other statement terminator such as in C or Pascal. On the other hand, it is not possible to write a number of short statements on one line.

F90 extension. In Fortran 90 the columns have lost their special meanings. Statements can begin in any column, and the number of columns is at most 132. Inline comments start with an "!" and extend to the end of the line. Continuations are indicated by an "&" at the end of an unfinished line (but before any inline comment); there can be 39 continuation lines. To group statements on one line, a ";" separator is used.

Program unit structure

Each Fortran program (and subprrogram) has a structure of a header line, a specification part of non-executable statements, the execution part, and an ending part. To be precise (and note that all of the following parts are optional):

Program unit header

The main program is supposed to start with a line

      PROGRAM myprog

where "myprog" is any name you want to give your program. This name is usually not used anywhere. Some systems let you add arguments to this line, eg to specify input/output channels or to pass command line arguments.

Other possible header keywords are FUNCTION or SUBROUTINE for subprograms, or BLOCK DATA for a common block initialisation.

Specification part

After the unit header there can be specification statements. These are not executable, though they can contain arithmetic expressions that can be evaluated at compile time.

Execution part

After any specification part follows the execution part. This is where the work gets done.

Program unit ending

Any type of program unit ends with an END line. Subprograms (with the exception of BLOCK DATA) additionally need a RETURN statement. Its recommended location is right before the END.