News  
   Publications  
Help  
Training  
Labs  
Contacts  
Connecting  
About  
Purchases  
Policies  
TTU Home  
DWMCC Home  

FORTRAN Tips

FORTRAN remains popular among engineers and scientists for numerical computation. These steps should make your FORTRAN programs easy to read, debug, and maintain:

Comment Your Programs
Precede every program, subroutine, and function with comments explaining what the module does, input and output variables, any unexpected side-effects, and other unusual features. Also, comment any group of statements whose function is not obvious.

Use Indentation and White Space
Indent statements inside of DO loops and IF-THEN-ELSE blocks to clearly show where control begins and ends. Leave blank lines between subroutines and between groups of statements performing different functions.

Explicitly Declare All Variables
Force yourself to explicitly declare all variables one of these two ways:
1. Compile your programs with the /WARNINGS= DECLARATIONS qualifier.
2. Include the statement IMPLICIT NONE before the variable declaration block in the main body of the program and each subroutine. (This is not standard, but it will work with most FORTRAN compilers.)

Minimize the Use of Common Variables
Common blocks can be useful when several subroutines must access the same set of information, but overuse of common blocks obscures the data flow between modules and increases the likelihood of unexpected side-effects. Pass all variables as subroutine parameters or function arguments unless the data will be shared by most modules in your program.

Avoid Excessive Use of GOTOs
Undisciplined use of GOTO statements results in unreadable "spaghetti" code. You can eliminate most GOTO statements by using control structures such as IF-THEN-ELSE blocks and WHILE loops. Use GOTOs only to implement a fundamental control structure not explicitly available in FORTRAN (such as the REPEAT UNTIL loop).

Don't Use Obsolete or Obscure Constructs
While arithmetic IFs (e.g IF (x) 10,20,30 ) and computed GOTOs (e.g. GOTO (12,24,36), I) are allowed, they are difficult for readers to decipher. In fact, arithmetic IFs are obsolete in Fortran 90.

Use a Unique Continue Statement for Each Do Loop

Modularize
Divide programs into separate modules (i.e. functions and subroutines), with each module responsible for a particular operation (e.g. data input, sort, output). Divide very large programs into several files, each containing those modules performing similar tasks. In cases requiring minor variations of a large program (you have several 5000-line programs that differ by, say, 100 lines), isolate the changeable sections of code in separate files and link the appropriate file with your main program as needed. Also, a constant appearing in more than one place should be declared as a PARAMETER.

Don't Try To Re-invent the Wheel
Rather than write your own mathematical or other library routines, use commercially-available ones if possible (such as those found in the IMSL library).

Avoid Portability Problems
Minimize system calls, VAX Run-Time Library functions, and any VAX extension statements to standard FORTRAN if you anticipate moving your program to another system. To check for non-standard syntax, compile your program with the /STANDARD qualifier. Do not write code that relies on assumptions about the the system architecture (e.g. don't assign anything to a character variable except characters).

For further reading, The Elements of Programming Style by Brian W. Kernighan and P. J. Plauger is recommended.

The Computing Connection, Vol 3, No 2, January 1993
D. W. Mattson Computer Center, Tennessee Technological University



This page maintained by: Lauren Neal
For additional information, contact Lauren Neal, LNeal@tntech.edu
Last updated: December 11, 1998