Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 39 ยท Getting Started

Program Structure and Source Form

Understand how a Fortran program is organized: specification and execution parts, program units, naming rules, free-form vs fixed-form source, and good style.

Fortran programs follow a strict but simple structure. Once you know it, every listing in this course reads the same way. This lesson explains the parts of a program unit, the rules for names and layout, the difference between modern free-form source and old fixed-form source, and the style conventions most teams follow.

The three parts of a program unit

A main program (and later, each procedure) has up to three parts, always in this order:

  1. Specification part: use statements, implicit none, and declarations of variables and constants.
  2. Execution part: the statements that do the work: assignments, if, do, print, calls.
  3. Internal subprogram part: optional procedures after contains.
FORTRAN
program structure_demo
  ! --- specification part ---
  implicit none
  integer, parameter :: n = 3
  integer :: i
  real :: total

  ! --- execution part ---
  total = 0.0
  do i = 1, n
    total = total + real(i)
  end do
  print *, "Sum of 1..3 =", total

contains
  ! --- internal subprogram part ---
  subroutine say_done()
    print *, "done"
  end subroutine say_done
end program structure_demo

You may not mix the parts: a declaration after an executable statement is an error. The contains keyword starts the internal procedures, which can see the program's variables. (Calling say_done is possible by adding call say_done() before contains; internal procedures are the subject of the procedures chapter.)

Program units

A Fortran application is built from program units, each of which can live in its own file:

Unit Purpose
program the main program, where execution starts (exactly one per application)
module a package of variables, constants, types and procedures to share
submodule the implementation part of a module
external subroutine / function stand-alone procedures (modules are preferred)
flowchart TD
  M["module geometry<br/>constants, procedures"] --> P["program main<br/>use geometry"]
  M2["module io_tools"] --> P

A main program imports modules with use and calls what they provide.

Rule of thumb: put every reusable procedure inside a module so the compiler can check argument types for you.

Names

A name (identifier) identifies a variable, procedure, program or type:

  • 1 to 63 characters
  • letters, digits and underscores
  • must start with a letter
  • case-insensitive: Count and count are the same name

Valid: total, x2, max_speed. Invalid: 2x (starts with a digit), my-var (hyphen), _tmp (starts with underscore).

Fortran has no reserved words in the C sense. You can technically name a variable if or real, but you must not: it makes code unreadable. Avoid names of intrinsic functions too (sum, max, size) because they hide the intrinsic in that scope.

Statements, lines and continuation

  • One statement per line is the norm.
  • A statement may be up to 132 characters per line, and continued on up to 255 lines with &.
  • Use ; to put several short statements on one line.
  • When a continued line is split inside a string, put an & at the start of the next line too:
FORTRAN
print *, "This long message continues &
         &on the next line"

Blank lines and extra spaces are ignored (outside strings), so you can align code for readability.

Free form versus fixed form

Fortran has two source forms, and mixing them is the source of many errors:

Free form (.f90) Fixed form (.f)
Era Fortran 90 and later FORTRAN 77 and earlier
Layout any column strict columns
Comments ! anywhere C or * in column 1
Continuation & at line end any character in column 6
Statement area any column columns 7 to 72
Spaces in names not allowed ignored (!)

In fixed form, columns 1 to 5 hold optional statement labels, column 6 is the continuation mark, columns 7 to 72 hold the code, and anything after column 72 is silently ignored, which truncated many a long line in the punch-card era. Here is the same idea in both forms:

FORTRAN
C     Fixed form: the C in column 1 makes a comment
      PROGRAM OLD
      REAL X
      X = 2.5
      PRINT *, X
      END
FORTRAN
! Free form: the same program
program new
  implicit none
  real :: x
  x = 2.5
  print *, x
end program new

You will meet fixed form when reading legacy libraries. Write all new code in free form.

Style conventions

Fortran does not force a style, but readable code follows these habits:

  • Lowercase keywords and names, two or three spaces of indentation inside blocks.
  • Always implicit none; always name end program, end subroutine and end function.
  • Descriptive names (wave_speed, not ws); constants in a parameter declaration.
  • One declaration group per concept; comment the why, not the what.
  • Use real(dp) style kinds rather than the compiler default (see kinds and precision).

Stand-ins for the structure

Python's structure is looser, but you can mirror the three parts with comments and a function, which makes the program easier to compare with its Fortran twin.

Python Stand-in (Python): specification, execution, internal procedure

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Stand-in (Python): valid and invalid names

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which order is correct inside a program unit?

    Declarations (specification part) come first, then executable statements, then optional internal procedures after contains.
  2. 2. Which of these is a valid Fortran name?

    Names start with a letter and contain only letters, digits and underscores.
  3. 3. What is the line limit for a free-form statement line?

    Free-form lines may hold up to 132 characters; use & to continue longer statements. 72 is the fixed-form limit.
  4. 4. How does fixed-form source mark a comment?

    In fixed form a C or * in column 1 makes the whole line a comment.
  5. 5. Which unit is the best home for reusable procedures?

    Procedures in modules get explicit interfaces, so the compiler checks calls for you.

Summary

  • A program unit has a specification part, an execution part and optional internal procedures after contains.
  • Programs are built from program, module, submodule and procedure units.
  • Names are 1 to 63 characters, start with a letter, and are case-insensitive.
  • Free form (.f90) is the modern style; fixed form (.f) is legacy with strict columns.
  • Write lowercase, indented code with implicit none and named end statements.

Next: Variables and Data Types