Free source form arrived with Fortran 90. Columns have no meaning: a statement can start anywhere on a line, and
blanks are now significant, as in other languages. It is the form of .f90 and its relatives, and of all new Fortran.
The rules are those of section 6.3.2 of the Fortran 2003 to 2023 standards (6.3.2 “Free source form”, 6.3.3 “Fixed source form”);
the numbers below are from the standards’ wording, which has changed little.
Lines and statements
- a line holds up to 132 characters in Fortran 90 to 2018, and up to 10 000 in Fortran 2023
- one or more statements may be on a line, separated by
;. A line may end with;, and an empty statement is allowed (x = 1;; y = 2) - a statement may be continued onto up to 255 further lines (Fortran 2008 and later; Fortran 90 and 95 allowed 39)
- blank lines may be anywhere, including between a statement and its continuation
Comments
A ! that is not inside a character literal (or a format’s character string) starts a comment that runs to the end of the line. It may
be on a line of its own or follow code. There is no block comment. A comment may follow the & of a continued line, and comment lines may stand between
a line and its continuation. A line that holds only a comment is not counted as one of the continuation lines.
Some comments are instructions to a compiler: !$omp, !$acc, !dir$, !gcc$
(see Directives and preprocessor).
Continuation with &
A line whose last character is an & (before an optional comment) goes on at the next line that is not a comment line.
If the first non-blank character of that line is also an &, the statement goes on from the character after it; this second
& is the only way to keep leading blanks when a character literal or a name is cut:
total = first_value + second_value + &
third_value ! a comment may follow the continued line
message = 'this literal goes on &
&over two lines'
call very_long_subroutine_name(argument_one, &
argument_two)
integer :: abc&
&def ! one name, abcdef
this literal goes on over two lines.- when the first line ends in a character literal, the literal runs to the
&and the second line must start with& - a token may be cut at an
&if the next line starts with&and carries on the token with no blank between - a
&is not a comment character, and in a!comment it is just text - in a character literal, the viewer colours a literal cut by
&as one string on both lines
Blanks
Blanks separate words in free form. A blank is required between adjacent names, keywords and constants
(integer x not integerx) and optional between the two words of these keyword pairs, which can also be written as one word:
BLOCK DATA, DOUBLE PRECISION, ELSE IF, ELSE WHERE, END ASSOCIATE,
END BLOCK, END BLOCK DATA, END CRITICAL, END DO, END ENUM, END FILE, END FORALL,
END FUNCTION, END IF, END INTERFACE, END MODULE, END PROCEDURE, END PROGRAM, END SELECT,
END SUBMODULE, END SUBROUTINE, END TEAM, END TYPE, END WHERE, GO TO, IN OUT,
SELECT CASE, SELECT TYPE, SELECT RANK.
No other pair may be run together, and blanks may not appear inside a name, a keyword or a numeric constant. The viewer colours each of these pairs as two keywords when they have a blank and as one when they do not.
Statement labels
A label is one to five digits (1 to 99999) before a statement, followed by a blank. It may be anywhere on the line and need not be in any order. A label may mark
any statement; it is used by do loops, go to, format references and the err=, end= and eor=
specifiers. Labels are unique inside a program unit. Free-form code written for Fortran 90 and later rarely needs one: end do, exit,
cycle, named constructs and internal procedures replace them. See Statements and control flow.
Construct names
A construct can be named by a name and a colon before its first statement, and the same name can follow its end, cycle and exit:
outer: do i = 1, n
inner: do j = 1, m
if (a(i, j) < 0) cycle outer
if (a(i, j) == 0) exit inner
end do inner
end do outer
cycle outer and exit inner say which loop they mean.Several forms in one project
A compiler reads each file in one form, chosen by its extension or a flag. The DEC and Intel compilers can switch forms inside a file with the
!DEC$ FREEFORM and !DEC$ NOFREEFORM directives. How the viewer decides is on File structure.