Welcome to AE Resources
Converted document Converted document

FORTRAN 90+: PROGRAM OUTLINE

These pages provide the basic outline for a free-form program. The order of information for every Fortran program is
PROGRAM program_name             ! Required
     USE module_name              ! This is only if modules are used.
     IMPLICIT NONE                ! Good programming practice.
     [declaration of variables]
     [execution code]             ! This is where the actual code occurs
     [subprogram]                 ! Discussed in later section.
 END PROGRAM program_name         ! Required
A program statement is the set of lines required at the beginning of every Fortran code so that the compiler knows how to handle the information to follow. The program statement includes the PROGRAM line, the USE line, IMPLICIT NONE and the declaration of variables. The first line of the code is the line that tells the compiler what kind of code is to follow. There are three options for the first line:
  • PROGRAM: A PROGRAM is a set of instructions that only produces an output. The parameters are pre-set and unchanging within the PROGRAM’s lines.
  • FUNCTION: A FUNCTION has changing input values based on what the user desires the FUNCTION to calculate at that moment. It can generally only serve one purpose and return one type of value. FUNCTIONs have some differences from PROGRAMs that warrant different sections to fully explain.
  • MODULE: A MODULE gives the user the ability to declare variables once for multiple programs, functions and subroutines in the same file.
The first line depends on the type of routine that is being written. Every program must have the PROGRAM statement:
PROGRAM code_name_no_spaces
The IMPLICIT NONE statement tells the compiler to not assume the type of variable based on the first letter of the variable. (Fortran 77 assumed variables starting with i-n are integers and a-h, o-z were real numbers.) This is extra work, but it can help in debugging as well. For example, if a variable is defined as name, but instead somewhere in the code it is mistyped as nmae, the compiler will produce an error instead of assuming a new variable, nmae. It is possible to code with IMPLICIT NONE not called, however, it is highly recommended that it is used.
The variables are then declared, and the actual code to perform the mathematics is added.
If subroutines are to be included in the program module (within the same file) as described at the beginning, they can be added per the description of functions and modules.
The program must end with a END PROGRAM program_name statement. The program can be self-contained within one file or multiple files can be compiled separately and linked.