Fortran/Program structure


Style

edit

Older versions of Fortran had strict guidelines on how a program was formatted. Fortran 90 lifted this restriction and would accept free format code as well as historical fixed format code.

Fixed format

edit

Prior to Fortran 90, source code followed a well-defined fixed format. Comments are indicated with a 'C' in the first column, columns 2-5 were reserved for an optional numerical statement label, a non-blank character in column 6 indicated the current line was a continuation from the previous one, and columns 7 through 72 were available for program statements. Columns 73 through 80 were ignored and often contained line sequence numbers. Blank lines were not allowed. This rigid formatting was the result of Fortran being developed in the era of batch computing and punched card input. The sequence number was used in the case a program 'deck' was dropped; program order could be recovered if the punch cards were placed in a card reader and sorted on columns 73-80. Compiler vendors offered extensions to this formatting, but it was rarely portable (for example, interpreting tab characters as 6 spaces.)

Note that while column position was significant, white space was not. The following program illustrates legal use of white space in fixed-format Fortran:

C2345678901234567890
      PROGRAM Z
      GOTO11
   11 CONTINUE
      GO TO 780
 780  CONTINUE
      G OTO3 60
 360  CONTINUE
      STOP
      END

While this code is technically legal, it is strongly encouraged to use white space to separate keywords, labels, and data to maintain readability.

Fortran was developed before the standardization of the ASCII character set and traditionally Fortran code has been written in all-caps. Variable names were limited to six characters, but this was often extended by compiler vendors.

Free format

edit

As of Fortran 90 and onward, source code does not require fixed column formatting. In this case, commands can freely start on any column. The 72 column limit has also been released. This allows for much more space for indentation.

program test
    implicit none
    integer four
    four = 4
    write (*,*) four
end program

Case sensitivity

edit

Fortran is not case sensitive. Fortran was typically used on systems that only supported capital letters. In fact, the language itself was called FORTRAN (in capitals). It remains customary, though completely unnecessary, to type Fortran commands in all capitals. This is useful to distinguish keywords in source code that is displayed on monocrome displays and print. These days, syntax highlighting is available to replace this. However it may be useful to visually distinguish older Fortran code from modern source code.

Whitespace

edit

Whitespace and empty lines usually won't matter in Fortran 90 or above. Some statements require whitespace, for example, program, function and subroutine require whitespace between the statement keyword and the program unit identifier.

However, unlike many other languages such as C, C++, and Java, the line delimiter ';' is optional, so each line of code may stay on its own line. However, the use of the command separator character ';' is discouraged.

Structure

edit

Program units

edit

Fortran programs are made up of program units. A single source code file can contain several program units but it is conventional to place each program unit in its own separate source code file. At their most basic they consist of a series of Fortran statements and conclude with the end statement.

Main program

edit

Every executable program must have a main program unit. For example, the following is a complete compilable and executable program.

write (*,*) "Hello, world"
end

However, it is much clearer to use the program statement to indicate that it is the main program unit.

program main
    write (*,*) "Hello, world!"
end program main

The main program is separated into sections. The first section should consist of module use statements. This is followed by implicit / implicit none statements that control whether undeclared variables are implicitly typed. This is followed by the declaration section where variables, types, interfaces and procedures are declared. Then comes the executable statements of the main program. The last section is the internal subprograms initiated by the contains statement.

program main
    ! Use statements section
    use module_name
    ! Implicit none statement section
    implicit none
    ! Declarations section
    integer :: a
    real :: b
    
    ! Executable section
    write (*,*) "Blah, blah, blah..."
end program main

Subprograms

edit

Program units may also be subprograms: these can be procedures (functions and subroutines), block data, modules or submodules.

External subprograms
edit

The following code shows a main program and a function. The increment function is external to the main program, and therefore needs a declaration in the main program on line 4.

program main
    implicit none
    integer :: a
    integer, external :: increment

    a = increment(34)
    write (*,*) a
end program main

function increment(input) result (output)
    implicit none
    integer :: output
    integer :: input

    output = input + 1
end function increment

However, the interface of the function is still implicit. To explicitly declare an external procedure, one can use an interface that declares all the inputs and outputs of external procedures. In which case, the main program would be as follows.

program main
    implicit none
    integer :: a
    interface
        integer function increment(input)
            integer :: input
        end function
    end interface

    a = increment(34)
    write (*,*) a
end program main
Internal subprograms
edit

Internal subprograms do not need an explicit interface or a declaration, because they are part of the parent program unit. A subprogram is internal if it is contained within the contains section of a program unit.

program main
    implicit none
    integer :: a

    a = increment(34)
    write (*,*) a
    
contains

    function increment(input) result (output)
        integer :: output
        integer :: input

        output = input + 1
    end function increment

end program main

Functions

edit

Subroutines

edit

Block Data

edit

Modules

edit

Submodules

edit