next up previous contents
Next: ConstantsVariables, and Arrays Up: Professional Programmer's Guide to Previous: Fortran in Practice

Program Structure and Layout

This section explains the rules for program construction and text layout. A complete Fortran program is composed of a number of separate program units. Each of these can contain both statements and comment lines. Statements are formed from items such as keywords and symbolic names. These in turn consist of characters.

The Fortran Character Set

The only characters needed to write Fortran programs, and the only ones that should be used in portable software, are those in the Fortran character set:


tabular213

tabular220
Although this character set is somewhat limited, it is at least universally available, which helps to make programs portable. What suffers is program legibility: lower-case letters are absent and it is necessary to resort to ugly constructions like .LT. and .GT. to represent operators like < and >. Some of the special characters, such as the asterisk and parentheses, are also rather overloaded with duties.

Blanks

The blank, or space, character is ignored everywhere in Fortran statements (except within character constants, which are enclosed in a pair of apostrophes). Although you do not need to separate items in Fortran statements with blanks, it is good practice to include a liberal helping of them since they improve legibility and often simplify editing. The only limitation (as explained below) is that statement lines must not extend beyond column 72.

Currency Symbol

The currency symbol has no fixed graphic representation: it appears on most systems as the dollar ``$'', but other forms such as ``tex2html_wrap_inline2897'' equally valid. This variability does not matter much because the currency symbol is not actually needed in Standard Fortran syntax.

Other Characters

Most computers have a character set which includes many other printable characters, for example lower-case letters, square brackets, ampersands and per-cent signs. Any printable characters supported by the machine may be used in comment lines and within character constants.

The Fortran character set does not include any carriage-control characters such as tab, carriage-return, or form-feed, but formatted WRITE statements can be used to produce paginated and tabulated output files.

Fortran programs can process as data any characters supported by the local hardware. The Fortran Standard is not based on the use of any particular character code but it requires its character comparison functions to use the collating sequence of the American Standard Code for Information Interchange (ASCII). Further details are given in section 7.6.

Statements and Lines

The statement is the smallest unit of a Fortran program, corresponding to what is called an instruction or command in some programming languages. Most types of statement start with a keyword which consists of one (or sometimes two) English words describing the main action of that statement, for example: READ, DO, ELSE IF, GO TO. Since blanks are ignored, compound keywords can be written either as one word or two: ELSEIF or ELSE IF (but the latter seems easier to read).

The rules for statement layout are an unfortunate relic of punched-card days. Every statement must start on a new line and each line is divided into three fixed fields:

Since labels and continuation markers are only needed on a few statements, the first six columns of most lines are left blank.

Any characters in column 73 or beyond are likely to be ignored (columns 73 to 80 were once used to hold card sequence numbers). This invisible boundary after column 72 demands careful attention as it can have very pernicious effects: it is possible for a statement to be truncated at the boundary but still be syntactically correct, so that the compiler will not detect anything wrong.

Continuation Lines

Statements do not have to fit on a single line. The initial line of each statement should have a blank in column 6, and all subsequent lines, called continuation lines, must have some character other than blank (or the digit zero) in column 6. Up to 19 continuation lines are allowed, i.e. 20 in total. The column layout needed with continuation lines is illustrated here:

 
columns 
123456789... 
      IF(REPLY .EQ. 'Y' .OR. REPLY .EQ. 'y' .OR. 
     $   REPLY .EQ. 'T' .OR. REPLY .EQ. 't') THEN
The currency symbol makes a good continuation marker since if accidentally misplaced into an adjacent column it would be almost certain to produce an error during compilation.

The END statement is an exception to the continuation rule: it may not be followed by continuation lines and no other statement may have an initial line which just contains the letters ``END". Neither rule causes many problems in practice.

Programs which make excessive use of continuation lines can be hard to read and to modify: it is generally better, if possible, to divide a long statement into several shorter ones.

Comment Lines

Comments form an important part of any computer program even though they are completely ignored by the compiler: their purpose is to help any human who has to read and understand the program (such as the original programmer six months later).

Comments in Fortran always occupy a separate line of text; they are marked by an asterisk in the first column. For example:

 
*Calculate the atmospheric refraction at PRESS mbar. 
       REF = PRESS * (0.1594 + 1.96E-2 * A + 2E-5 * A**2) 
*Correct for the temperature T (Celsius) 
       TCOR = (273.0 + T) * (1.0 + 0.505 * A + 8.45E-2 * A**2)
A comment may appear at any point in a program unit except after the END statement (unless another program unit follows, in which case it will form the first line of the next unit). A completely blank line is also allowed and is treated as a blank comment. This means that a blank line is not actually permitted after the last END statement of a program.

There is no limit to the number of consecutive comment lines which may be used; comments may also appear in the middle of a sequence of continuation lines. To conform to the Fortran Standard, comment lines should not be over 72 characters long, but this rule is rarely enforced.

Comments may include characters which are not in the Fortran character set. It helps to distinguish comments from code if they are mainly written in lower-case letters (where available). It is also good practice for comments to precede the statements they describe rather than follow them.

Some systems allow end-of-line comments, usually prefaced by an exclamation mark: this is not permitted by the Fortran standard. For compatibility with Fortran66 comments can also be denoted by the letter C in column 1.

Statement Labels

A label can be attached to any statement. There are three reasons for using labels:

Example:

 
*Read numbers from input file until it ends, add them up. 
      SUM = 0.0 
100   READ(UNIT=IN, FMT=200, END=9999) VALUE 
200   FORMAT(F20.0) 
      SUM = SUM + VALUE 
      GO TO 100 
9999  WRITE(UNIT=*, FMT=*)'SUM of values is', SUM
Each label has the form of an unsigned integer in the range 1 to 99999. Blanks and leading zeros are ignored. The numerical value is irrelevant and cannot be used in a calculation at all. The label must appear in columns 1 to 5 of the initial line of the statement. In continuation lines the label field must be blank.

A label must be unique within a program unit but labels in different program units are quite independent. Although any statement may be labelled, it only makes sense to attach a label to a FORMAT statement or an executable statement, since there is no way of using a label on any other type of statement.

Statement labels are unsatisfactory because nearly all of them mark a point to which control could be transferred from elsewhere in the program unit. This makes it much harder to understand a program with many labelled statements. Unfortunately at present one cannot avoid using labels altogether in Fortran. If labels are used at all they should appear in ascending order and preferably in steps of 10 or 100 to allow for changes. Labels do not have to be right-justified in the label field.

Program Units

A complete executable program consists of one or more program units. There is always one (and only one) main program unit: this starts with a PROGRAM statement. There may also be any number of subprogram units of any of the three varieties:

Subroutines and external functions are known collectively as external procedures; block data subprograms are not procedures and are used only for the special purpose of initialising the contents of named common blocks.

Every program unit must end with an END statement.

Procedures

Subroutines and external functions are collectively known as external procedures: they are described in full in section 9. A procedure is a self-contained sequence of operations which can be called into action on demand from elsewhere in the program. Fortran supplies a number of intrinsic functions such as SIN, COS, TAN, MIN, MAX, etc. These are procedures which are automatically available when you need to use them in expressions. External functions can be used in similar ways: there may be any number of arguments but only one value is returned via the function name.

The subroutine is a procedure of more general form: it can have any number of input and output arguments but it is executed only in response to an explicit CALL statement.

Procedures may call other procedures and so on, but a procedure may not call itself directly or indirectly; Fortran does not support recursive procedure calls.

Most Fortran systems allow procedures to be written in other languages and linked with Fortran modules into an executable program. If the procedure interface is similar to that of a Fortran subroutine or function this presents no problem.

The normal way to transfer information from one program unit to another is to use the argument list of the procedure as described in section 9, but it is also possible to use a common block: a shared area of memory. This facility, which is less modular, is described in section 12.

Statement Types and Order

Fortran statements are either executable or non-executable. The compiler translates executable statements directly into a set of machine code instructions. Non-executable statements are mainly used to tell the compiler about the program; they are not directly translated into machine code. The END statement is executable and so are all those in the lowest right-hand box of the table below; all other statements are non-executable.

The general order of statements in a program unit is:

The table below shows shows the complete statement ordering rules: the statements listed in each box can be intermixed with those in boxes on the same horizontal level (thus PARAMETER statements can be intermixed with IMPLICIT statements) but those in boxes separated vertically must appear in the proper order in each program unit (thus all statement functions must precede all executable statements).


tabular288

Execution Sequence

A program starts by executing the first executable statement of the main program unit. Execution continues sequentially unless control is transferred elsewhere: an IF or GO TO statement, for example, may transfer control to another part of the same program unit, whereas a CALL statement or function reference will transfer control temporarily to a procedure.

A program continues executing until it reaches a STOP statement in any program unit, or the END statement of the main program unit, or until a fatal error occurs. When a program terminates normally (at STOP or END) the Fortran system closes any files still open before returning control to the operating system. But when a program is terminated prematurely files, especially output files, may be left with incomplete or corrupted records.

Symbolic Names

Symbolic names can be given to items such as variables, arrays, constants, functions, subroutines, and common blocks. All symbolic names must conform to the following simple rule: the first character of each name must be a letter, this may be followed by up to five more letters or digits. Here are some examples of valid symbolic names:

 
          I  MATRIX  VOLTS  PIBY4  OLDCHI  TWOX  R2D2  OUTPUT
And here are some names which do not conform to the rules:
tabular328
It is best to avoid using digits in names unless the meaning is clear, because they are often misread. The digit 1 is easily confused with the letter I, similarly 0 looks much like the letter O on many devices.

The six-character limit on the length of a symbolic name is one of the most unsatisfactory features of Fortran: programs are much harder to understand if the names are cryptic acronyms or abbreviations, but with only six characters there is little choice. Although many systems do not enforce the limit (and Fortran90 allows names up to 31 characters long), at present the only way to ensure software portability is to keep to it strictly. There is a further problem with items which have an associated data type (constants, variables, arrays, and functions). Unless the data type is declared explicitly in a type statement, it is determined by the initial letter of the name. This may further restrict the choice.

Scope of Symbolic Names

Symbolic names which identify common blocks and program units of all types are global in scope, i.e. their name must be unique in the entire executable program. Names identifying all other items (variables, arrays, constants, statement functions, intrinsic functions, and all types of dummy argument) are local to the program unit in which they are used so that the same name may be used independently in other program units.

To see the effect of these rules here is a simple example. Suppose your program contains a subroutine called SUMMIT. This is a global name so it cannot be used as the name of global item (such as an external procedure or a common block) in the same executable program. In the SUMMIT subroutine and in any other program unit which calls it the name cannot be used for a local item such as a variable or array. In all other program units, however, including those which call SUMMIT indirectly, the name SUMMIT can be used freely e.g. for a constant, variable, or array.

The names of global items need to be chosen more carefully because it is harder to alter them at a later stage; it can be difficult to avoid name clashes when writing a large program or building a library of procedures unless program unit names are allocated systematically. It seems appropriate for procedures to have names which are verb-like. If you find it difficult to devise sensible procedure names remember that the English language is well stocked with three and four-letter verbs which form a good basis, for example: DO, ASK, GET, PUT, TRY, EDIT, FORM, LIST, LOAD, SAVE, PLOT. By combining a word like one of these with one or two additional letters it is possible to make up a whole range of procedure names.

Reserved Words

In most computer languages there is a long list of words which are reserved by the system and cannot be used as symbolic names: Cobol programmers, for example, have to try to remember nearly 500 of them. In Fortran there are no reserved words. Some Fortran keywords (for instance DATA, END, and OPEN) are short enough to be perfectly valid symbolic names. Although it is not against the rules to do this, it can be somewhat confusing.

The names of the intrinsic functions (such as SQRT, MIN, CHAR) are, technically, local names and there is nothing to prevent you using them for your own purposes, but this is not generally a good idea either. For example, if you choose to use the name SQRT for a local variable you will have more difficulty in computing square-roots in that program unit. It is even more unwise to use the name of an intrinsic function as that of an external procedure because in this case the name has to be declared in an EXTERNAL statement in every program unit in which it is used in this way.

PROGRAM Statement

The PROGRAM statement can only appear at the start of the main program unit. Its only function is to indicate what type of program unit it is and to give it symbolic name. Although this name cannot be used anywhere else in the program, it may be used by the Fortran system to identify error messages etc. The general form is simply:
PROGRAM name
Where name is a symbolic name. This name is global in scope and may not be used elsewhere in the main program nor as a global name in any other program unit. For compatibility with Fortran66 the PROGRAM statement is optional. This can have unexpected effects: if you forget use a SUBROUTINE or FUNCTION statement at the start of a procedure the compiler will assume it to be a (nameless) main program unit. Since this will normally result in two main program units, the linker is likely to detect the mistake.

END Statement

The END statement must appear as the last statement of every program unit. It simply consists of the word:
END
which may not be followed by any continuation lines (or comments). The END statement is executable and may have a label attached. If an END statement is executed in a subprogram unit, i.e. a procedure, it returns control to the calling unit; if an END statement is executed in the main program it closes any files which are open, stops the program, and returns control to the operating system.


next up previous contents
Next: ConstantsVariables, and Arrays Up: Professional Programmer's Guide to Previous: Fortran in Practice

Clive Page
Tue Feb 27 11:14:41 GMT 2001