-
Notifications
You must be signed in to change notification settings - Fork 115
Source form
All new code must be written in free form. If you are doing extensive work on an existing fixed form file you should consider converting it to free form. Continuing to work on existing fixed form files is allowed.
All code blocks must be indented. Fortran aware text editors, such as (X)emacs, nedit and vim, have assisted or automatic indentation features which make this task much easier. If you find that much of the horizontal space on each line is taken up with indenting, this is an indication that the current routine may be too complicated and perhaps should be split into multiple routines.
Fortran has two incompatible source layout rules. Fixed form source is the older and was introduced in the era of punch cards. In this source form, the first five characters on each line are reserved for a line number, column 6 is used to indicate continuation lines and code begins at column 7. Code is only permitted in the first 80 columns, although compilers often (always?) support options to extend this.
Free form source was introduced with Fortran 90 and is often mistakenly called Fortran 90 style source. In fact, it is equally possible to write Fortran 90 (or 95 or 2003) in free or fixed form. To add to the confusion, free form Fortran files are usually identified by a .f90
or .F90
suffix. Note that free form Fortran 95 and Fortran 2003 also use the .[fF]90
syntaxes. The use of a capital F in a file name extension (free or fixed form) indicates to the compiler that the preprocessor should be run on the file.
Free form source is more flexible, easier to use and makes more efficient use of the columns of your screen than fixed form source. The following sections detail particular rules of the different source forms.
Free form comments begin with an exclamation mark (!). Both full line comments and trailing comments are permitted and there are no restrictions over the column in which the exclamation mark may be placed. Comments allways extend to the end of the line and do not continue to the next line (although the next line may also be a comment if it has its own exclamation mark). ! This is a whole line comment. if (foo==bar) then ! This is a trailing comment. Fixed form comments begin with the letter C in column one and continue for the rest of the line.
Free form lines which end with an ampersand (&) continue on the next line. For example:
x = y + dot_product(vector_1, &
vector_2)
Where it is necessary to break a line at a point at which spaces are not permitted or would have an undesirable effect (such as in the middle of a name or in a string) then a second ampersand on the second line will cause the interviening whitespace to be ignored by the compiler:
message= "A very long message which will not&
& fit on one line"
Fixed form continuation lines are marked by any non-blank character on line 6. Unlike free form source, there is no continuation mark on the line to be continued.
In free form source, any line numbers need only be the first non-blank characters on a line. There are no columns reserved for line numbers. Free form source and modern programming practice removes most of the need for line numbers. In particular, labelled loops remove one of the main uses of the goto
statement. Line numbers are usually only used for trapping I/O errors.
Fixed form line numbers must occupy the first five columns of the line and, apart from the comment marker, they are the only non-blank characters permitted in those columns.