Fortran program structure
last modified November 14, 2026
A Fortran program is a collection of program units. Every program has exactly one main program, and it may also have modules and external procedures. This article describes the parts of a main program, the order in which statements must appear, the rules of free-form source, and the way a program ends.
A complete program
The listing below has all the parts of a typical main program: a
use statement, implicit none, declarations,
executable statements, and an internal function after the
contains statement.
program structure
use, intrinsic :: iso_fortran_env, only: output_unit
implicit none
integer :: total
total = add(2, 3)
write (output_unit, *) 'Total:', total
contains
integer function add(a, b)
integer, intent(in) :: a, b
add = a + b
end function add
end program structure
The part before the first executable statement is the specification
part. It holds the use statement, which makes the unit
output_unit of the intrinsic module iso_fortran_env
available, then implicit none, and then the declarations. The
execution part follows: the assignment and the write
statement. Everything after contains are internal procedures,
here the function add, which the main program can call and which
can see the variables of the main program. The write statement
with the unit output_unit and the format * prints
the same way as print *.
$ gfortran structure.f90 -o structure $ ./structure Total: 5
Free-form source
Fortran 90 introduced free-form source, and it is the only layout used in new
code. Statements may start in any column, indentation is up to the author, and
a line may hold up to 132 characters. A longer line is an error in gfortran
("Line truncated"); the option -ffree-line-length-none removes the
limit, but a program that needs it is usually better off with continuation
lines.
Fortran is case-insensitive. PROGRAM, program and
Program are the same keyword, and Count,
COUNT and count are the same variable. The usual
convention is lower case for everything, which is what this series uses. The
next program mixes the cases on purpose to show that the compiler does not
mind.
PROGRAM Casing
IMPLICIT NONE
Integer :: Count
count = 10
COUNT = cOuNt + 5
Print *, count
End Program CASING
The program declares Count, assigns to count and
COUNT, and prints count. All four spellings denote one
variable, so the result is 10 + 5. Mixed case like this makes a
program hard to read, but it does appear in old code, which is why it is worth
knowing.
$ gfortran casing.f90 -o casing
$ ./casing
15
Comments
A comment starts with an exclamation mark and runs to the end of the line. The mark may stand at the start of a line or after a statement. Fortran has no block comment, so a commented-out region needs the mark on every line. An exclamation mark inside a character literal is part of the text.
program comments
implicit none
integer :: n
! a comment on its own line
n = 42 ! a comment after a statement
print *, 'n = ', n, ' ! not a comment'
! n = 0
end program comments
The program has a comment on its own line, a comment after a statement, and a
commented-out assignment at the end. The text ' ! not a comment'
is printed in full, because the mark is inside quotes and the compiler does
not treat it as the start of a comment.
$ gfortran comments.f90 -o comments $ ./comments n = 42 ! not a comment
Order of statements
Statements of a program unit come in a fixed order: use
statements first, then implicit none, then declarations, then
executable statements, and finally contains with the internal
procedures. A declaration that follows an executable statement is an error.
program order
implicit none
integer :: x
x = 5
integer :: y
y = x
print *, y
end program order
Here the declaration of y follows the assignment
x = 5. The compiler reports the misplaced declaration, and, as a
consequence, it also complains that y has no type. The
block construct, shown in the
variables article, is the way to declare a
variable in the middle of the executable part.
$ gfortran order.f90 -o order
order.f90:5:16:
5 | integer :: y
| 1
Error: Unexpected data declaration statement at (1)
order.f90:6:5:
6 | y = x
| 1
Error: Symbol ‘y’ at (1) has no IMPLICIT type
Continuation lines and several statements per line
A statement that does not fit on one line continues on the next one when the first line ends with an ampersand. A semicolon separates several statements on one line. A character literal that is split across lines needs an ampersand at the end of the first line and another one at the start of the next, because otherwise the blanks that make up the indentation would become part of the text.
program continuation
implicit none
integer :: a, b, c, total
a = 1; b = 2; c = 3
total = a + &
b + &
c
print *, 'Total:', total
print *, 'A long text that does not fit &
&on one line'
end program continuation
The first line assigns three variables, separated by semicolons. The sum is
written over three lines, each ending in &. The long text is
split in the middle of the literal: the second line begins with
&, and the text continues at the character that follows it,
so the printed line contains no extra spaces.
$ gfortran continuation.f90 -o continuation $ ./continuation Total: 6 A long text that does not fit on one line
Naming the end
The closing statement of a program unit can repeat its name:
end program name, end function name,
end subroutine name, end module name. The name is
optional, but when it is given the compiler checks it. A copy-and-paste
mistake, such as a stale name, is caught at once.
program demo
print *, 'Hello'
end program wrong
The program is called demo but ends with
end program wrong. The compiler stops with an error that names
the expected label. For that reason the series always repeats the name.
$ gfortran wrongend.f90 -o wrongend
wrongend.f90:3:17:
3 | end program wrong
| 1
Error: Expected label ‘demo’ for END PROGRAM statement at (1)
f951: Error: Unexpected end of file in ‘wrongend.f90’
Stopping a program
A program ends when execution reaches its end program statement.
The stop statement ends it earlier, from anywhere, including from
inside a procedure. A plain stop exits with status 0. With an
integer code, stop 3 prints STOP 3 on the standard
error stream and exits with that status, which the shell shows in
$?. The statement error stop ends the program as a
failure; it always exits with a non-zero status and prints a backtrace.
program stopcode
print *, 'About to stop'
stop 3
print *, 'Never printed'
end program stopcode
The program prints one line, stops with the code 3, and never reaches the last
print. The compiler does not complain about the unreachable
statement in this case. After the run, echo $? prints the exit
status that the shell received.
$ gfortran stopcode.f90 -o stopcode $ ./stopcode About to stop STOP 3 $ echo $? 3
A program with a module
Larger programs split their code into modules. A module collects procedures,
constants and types in a separate program unit that other units import with
use. A module is not executed; the main program calls what it
exports. A source file may hold several program units, and a module must appear
before the units that use it.
module greetings
implicit none
private
public :: greet
contains
subroutine greet(name)
character(len=*), intent(in) :: name
print *, 'Hello, ', name, '!'
end subroutine greet
end module greetings
program main
use greetings, only: greet
implicit none
call greet('Fortran')
end program main
The module greetings declares everything private by default and
makes only greet public. The main program imports just that name
with use greetings, only: greet and calls the subroutine with
call. When the module is in the same file, as here, one command
compiles both units. As a side effect gfortran writes a
greetings.mod file, which holds the interface of the module for
other source files; separate compilation of modules is covered in a later
article.
$ gfortran greetings.f90 -o greetings $ ./greetings Hello, Fortran!
Next steps
The variables article describes
declarations, implicit none, and constants. Fixed-form source,
the layout in which columns matter, is described in the article on FORTRAN 77
and legacy code.
Source
GNU Fortran: Options controlling Fortran dialect
Fortran 2018 standard (J3/18-007r1)
In this article we have looked at the structure of a Fortran program: its program units, the order of statements, free-form source, comments, continuation lines, and the ways a program ends. The previous article, Introduction to Fortran, covers installing the compiler, and the next one, Fortran variables and declarations, shows how data is declared and initialised.
Author
List all Fortran tutorials.