free_source_form(7f) - [FORTRAN] syntax of free source form statements, comments, and continuation lines
[label] statement [! comment]
There are two source forms: free and fixed. Fixed-format is generally considered deprecated but is commonly encountered in older codes, as all FORTRAN pre-Fortran90 was fixed-form. All new code should be generated as free-format without some overwhelming reason, such as the need to use fixed-form INCLUDE files, or where it is required to be compatible with very old compilers.
Free form and fixed form may not be mixed in the same program unit (_there are often compiler-specific extensions that allow this but
In free source form there are no restrictions on where a statement (or portion of a statement) may appear within a line. A line may contain zero characters. If a line consists entirely of characters of default kind, it may contain at most 132 characters. If a line contains any character that is not of default kind (like a tab character or unicode), the maximum number of characters allowed on the line is processor dependent.
it is non-standard_). The means for specifying the source form of a program unit are processor dependent. Most commonly, if the file ends if .F or .f it is assumed to be fixed-form; a suffix of .f90 or .F90 typically indicates the file is free-format. The upper-case suffix usually additionally indicates the file should be run through a pre-processor (which one -- cpp, fpp, m4, ... varies with different programming environments).
In free source form blank characters shall not appear within lexical tokens other than in a character context or in a format specification. Blanks may be inserted freely between tokens to improve readability; for example, blanks may occur between the tokens that form a complex literal constant. A sequence of blank characters outside of a character context is equivalent to a single blank character.
A blank shall be used to separate names, constants, or labels from adjacent keywords, names, constants, or labels. For example, the blanks after REAL, READ, 30, and DO are required in the following:
> REAL X > READ 10 > 30 DO K=1,3One or more blanks shall be used to separate adjacent keywords except in the following cases, where blanks are optional:
Adjacent keywords where separating blanks are optional
> ALL STOP > END IF > BLOCK DATA > END MODULE > DOUBLE PRECISION > END INTERFACE > ELSEIF > END PROCEDURE > ELSE WHERE > END PROGRAM > END ASSOCIATE > END SELECT > END BLOCK > END SUBMODULE > END BLOCK DATA > END SUBROUTINE > END CRITICAL > END TYPE > END DO > END WHERE > END ENUM > GO TO > END FILE > IN OUT > END FORALL > SELECT CASE > END FUNCTION > SELECT TYPE
The character "!" initiates a comment except where it appears within a character context. The comment extends to the end of the line. If the first nonblank character on a line is an "!", the line is a comment line. Lines containing only blanks or containing no characters are also comment lines. Comments may appear anywhere in a program unit and may precede the first statement of a program unit or may follow the last statement of a program unit. Comments have no effect on the interpretation of the program unit.
NOTE 3.6 This part of ISO/IEC 1539 does not restrict the number of consecutive comment lines.
The character "&" is used to indicate that the current statement is continued on the next line that is not a comment line. Comment lines cannot be continued; an "&" in a comment has no effect. Comments may occur within a continued statement. When used for continuation, the "&" is not part of the statement. No line shall contain a single "&" as the only nonblank character or as the only nonblank character before an "!" that initiates a comment.
If a noncharacter context is to be continued, an "&" shall be the last nonblank character on the line, or the last nonblank character before an "!". There shall be a later line that is not a comment; the statement is continued on the next such line. If the first nonblank character on that line is an "&", the statement continues at the next character position following that "&"; otherwise, it continues with the first character position of that line.
If a lexical token is split across the end of a line, the first nonblank character on the first following noncomment line shall be an "&" immediately followed by the successive characters of the split token.
If a character context is to be continued, an "&" shall be the last nonblank character on the line and shall not be followed by commentary. There shall be a later line that is not a comment; an "&" shall be the first nonblank character on the next such line and the statement continues with the next character following that "&".
If a statement is not continued, a comment or the end of the line terminates the statement.
A statement may alternatively be terminated by a ";" character that appears other than in a character context or in a comment. The ";" is not part of the statement. After a ";" terminator, another statement may appear on the same line, or begin on that line and be continued. A sequence consisting only of zero or more blanks and one or more ";" terminators, in any order, is equivalent to a single ";" terminator.
A label may precede any statement not forming part of another statement (Note that no Fortran statement begins with a digit).
It is strongly suggested that numeric labels only be used on CONTINUE statements and for labeling FORMAT statements.
A statement shall not have more than 255 continuation lines.
Sample program:
program demo_free_source_form use,intrinsic :: iso_fortran_env, only : ERROR_UNIT, & INPUT_UNIT, & OUTPUT_UNIT ! access computing environment use iso_fortran_env, only : int8, int32, int64 implicit none integer,parameter :: dp=kind(0.0d0) real(kind=dp),save :: x(10)=0.0_dp character(len=255) :: filename logical :: lval integer :: ier integer :: i_myloop integer :: i, j, k character(len=*),parameter :: VERSION=1.0 character(len=:), allocatable :: mystring call usage() filename=my file print *, "filename=", trim(filename) !----------------------------------------------------------------------- MYLOOP: do I_MYLOOP=1,10 ! DO loop cycle MYLOOP ! start next pass of loop exit MYLOOP ! go to next statement after corresponding ENDDO enddo MYLOOP !----------------------------------------------------------------------- block character(len=1) :: c mystring=trim(filename) do i=1,len(mystring) c=mystring(i:i) select case (c) CASE (a : j); WRITE(*,*)c, :One of the first ten letters CASE (l : p, u : y); WRITE(*,*)c, :One of l, m, n, o, p, u, v, w, x, y CASE (z, q : t); WRITE(*,*)c, :One of z, q, r, s, t CASE default WRITE(ERROR_UNIT,*)c, :Other characters, which may not be letters end select enddo write(*,*)signum([10,20,0,-100]) ! mine(3f) is a function that does I/O. Do not use in an I/O statement x=mine(100.40) endblock !----------------------------------------------------------------------- contains ! An integer signum function: elemental integer function signum (n) integer,intent(in) :: n select case (n) case (:-1); signum = 1 case (0); signum = 0 case (1:); signum = 1 end select end function signum !------------------------------------------------------------------------------- function mine(xx) result(yy) ! note when RESULT used, function name has no type implicit none real :: xx real :: yy write(*,*)VALUE=,xx yy=xx end function mine !------------------------------------------------------------------------------- subroutine usage() character(len=80),allocatable :: help_text(:) integer :: i help_text=[ & &12345678901234567890123456789012345678901234567890123456789012345678901234567890,& &This is example help text for the example program ,& & ,& & ,& & ] WRITE(*,(a))(help_text(i),i=1,size(help_text)) end subroutine usage !------------------------------------------------------------------------------- end program demo_free_source_form !-------------------------------------------------------------------------------
Nemo Release 3.1 | free_source_form (7) | February 23, 2025 |