« previous next»

2.2    Program Format

In general, a Fortran program should have the following form:

HEADING - The heading has the form PROGRAM name, where name is any legal identifier (an identifier includes any of the legal Fortran characters (A1.1) but without blank spaces.  Program names must begin with a letter, followed by up to thirty letters, digits, or underscores.  This is not mandatory, but it is highly recommended (meaning points will be taken off if you do not have a program name).  Use a name that has some relation to the program task.  This way it can assist you or others when looking at your code in the future. A program name will distinguish your code from other codes, modules, and subprograms.

IMPLICIT NONE - Although not required for a program to run and compile this statement is required in any program written for this course.  Most languages require that all variables be specified; however, Fortran will assign a type to any variable whose type is not declared.  IMPLICIT NONE cancels this naming convention.  This will help guard against errors associated with variable type that occur due to the default type convention implicit in Fortran.

SPECIFICATION SECTION - Following the IMPLICIT NONE declaration it is important to declare the environment of the program.  All variables and constants need to be explicitly declared in this portion of the program.  The data type must be stated for all constants, variables, parameters, etc.

EXECUTION SECTION - This is the section where the program actions are specified.

INTERNAL SUBPROGRAMS - If needed subprograms can be contained within the body of the program (internally).

END PROGRAM - This is the only required part of the program.  It indicates to the compiler the end of the program and also halts execution.  This statement can be labeled.

Here is a simple example program.

    PROGRAM introduction       !  heading with name = introduction

    IMPLICIT NONE                !  statement that all data types must be declared

    !  Declare variables and constants in the specification section

    INTEGER :: age = 26                       !  age is integer type data and has the initial value of 26
    CHARACTER (4) :: name = "Eric"    !  name is character type, length 4, and is Eric

    !  Now here is the execution section.
    !  The output is:  My name is Eric and I am 26 years old.

    PRINT *, "My name is ", name, " and I am ", age, "years old."

    !  There are no internal subprograms in this program

    !  Here is the END PROGRAM statement.
    !  It has been labeled with the program name.

    END PROGRAM introduction

« previous next»