Fortran Source code files


A source code file is a (generally) plain text file which is to processed by the compiler. A source code file may contain up to one main program and any number of modules and external subprograms. For example, a source code file may contain the following

module mod1
end module mod1

module mod2
end module mod2

function func1()    ! An external function
end function func1

subroutine sub1()   ! An external subroutine
end subroutine sub1

program prog        ! The main program starts here...
end program prog    ! ... and ends here

function func2()    ! An external function
end function func2

We should recall here that, even though the external subprograms are given in the same file as the modules and the main program, the external subprograms are not explicitly known by any other component.

Alternatively, the individual components may be spread across multiple files, and even compiled at different times. Compiler documentation should be read on how to combine multiple files into a single program.

A single source code file may contain either fixed-form or free-form source code: they cannot be mixed, although multiple files being combined at compile-time may have different styles.

To indicate to the compiler the source form there are generally two options:

  • choice of filename suffix
  • use of compiler flags

The compile-time flag to indicate fixed- or free-form source can be found in the compiler's documentation.

The significant filename suffixes are also to be found in the compiler's documentation, but as a general rule a file named file.f90 is taken to contain free-form source whereas the file file.f is taken to contain fixed-form source.

The use of .f90 suffix to indicate free-form source (which was introduced in the Fortran 90 standard) often tempts the programmer to use the suffix to indicate the language standard to which the source code conforms. For example, we may see files with .f03 or .f08 suffixes. This is generally to be avoided: most Fortran 2003 source is also compliant with Fortran 77, Fortran 90/5 and Fortran 2008. Further, many comilers don't automatically consider such suffixes.

Compilers also often offer a built-in code preprocessor (generally based on cpp). Again, a compile-time flag may be used to indicate that the preprocessor should be run before compilation, but the source code file suffix may also indicate such preprocessing requirement.

For case-sensitive filesystems the file file.F is often taken to be a fixed-form source file to be preprocessed and file.F90 to be a free-form source file to be preprocessed. As before, the compiler's documentation should be consulted for such flags and file suffixes.