Fortran/Fortran structure and style

Style
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
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:

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
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.

Case sensitivity
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
Whitespace and empty lines usually won't matter in Fortran 90 or above. Some statements require whitespace, for example,,   and   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.

Program units
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  statement.

Main program
Every executable program must have a main program unit. For example, the following is a complete compilable and executable program. However, it is much clearer to use the  statement to indicate that it is the main program unit. The main program is separated into sections. The first section should consist of module  statements. This is followed by  /   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  statement.

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

External subprograms
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. 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.

Internal subprograms
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  section of a program unit.