Fortran/Fortran structure and style< Fortran
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.
Prior to Fortran 90, Fortran 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:
PROGRAM Z C2345678901234567890 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.
Originally Fortran required all statements to be in ALL CAPS, and in fact the language itself was called FORTRAN (in caps). It remains a bit customary, though completely unnecessary, to put Fortran commands in ALL CAPS.
PROGRAM test IMPLICIT NONE INTEGER four four = 4 WRITE (*,*) four END PROGRAM
Whitespace and empty lines usually won't matter in F90 or above, but in rare cases errors will pop up. 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! Note that the use of the command separator character ';' is strongly discouraged.
- There is a GOTO command, but don't use it too much.
- Use DO loops early and often.
- Encapsulate your code into functions and subroutines making it easier to program at higher levels, and allowing others to understand whats going on.
- Comment your code well. Don't comment every line, but explain how everything works.