TExcelTAD
Home › Fortran › Fortran Syntax

Fortran Syntax

Modern Fortran uses free-form source: you can start statements in any column, indent however you like and write lines up to 132 characters. This lesson covers the small set of rules that govern how code is written.

Case insensitivity

Fortran ignores letter case in keywords and names. PRINT, Print and print are the same statement, and Total, TOTAL and total are the same variable. The modern convention is lower case for everything, which is what this course uses.

fortran · try it yourself
program case_demo
  implicit none
  integer :: Count
  count = 3              ! same variable as Count
  COUNT = COUNT + 1      ! still the same variable
  PRINT *, count
end program case_demo
           4

Only the contents of character strings are case sensitive: 'Hello' and 'HELLO' are different strings.

Names

Names (of variables, procedures, programs …) must start with a letter and may contain letters, digits and underscores, up to 63 characters. speed_of_light, x2 and nParticles are valid; 2x and my-var are not. Fortran has no reserved words, so you could call a variable end or if, but please don't.

One statement per line

Each line normally holds one statement. A semicolon lets you put several short statements on one line, and an ampersand & at the end of a line continues the statement on the next line:

fortran · try it yourself
program layout
  implicit none
  real :: a, b, c, total
  a = 1.5; b = 2.5; c = 3.0          ! three statements on one line

  total = a + &
          b + &
          c                          ! one statement over three lines
  print *, 'Total:', total

  print *, 'A long message can be split ', &
           'across lines as two items.'
end program layout
 Total:   7.00000000    
 A long message can be split across lines as two items.

Inside a string, the continuation needs an & at the end of the first line and at the start of the next, otherwise the indentation becomes part of the string:

fortran · try it yourself
program split_string
  implicit none
  print '(a)', 'This sentence is written on two &
               &source lines.'
end program split_string
This sentence is written on two source lines.

Comments

Everything after ! on a line is a comment (unless the ! is inside a string). There are no block comments; just start each line with !.

fortran
! Compute the area of a circle.
! Input: radius r (metres)
area = pi * r**2    ! r**2 means r squared

Program structure

A Fortran program unit always follows the same order:

fortran
program name
  use some_module       ! 1. use statements (modules)
  implicit none         ! 2. implicit none
  integer :: n          ! 3. declarations
  real :: x

  n = 10                ! 4. executable statements
  x = n / 4.0
  print *, x

contains                ! 5. optional internal procedures
  ! functions and subroutines used only by this program
end program name

Declarations must come before the first executable statement. Mixing them, as you might in C or Python, is a compile error:

fortran · try it yourself
program wrong_order
  implicit none
  integer :: a
  a = 1
  integer :: b      ! declaration after a statement: not allowed
  b = 2
  print *, a + b
end program wrong_order
/app/example.f90:5:64:

    5 |   integer :: b      ! declaration after a statement: not allowed
      |                                                                1
Error: Unexpected data declaration statement at (1)
/app/example.f90:6:3:

    6 |   b = 2
      |   1
Error: Symbol 'b' at (1) has no IMPLICIT type

Read compiler errors from the top: the first message is usually the real cause; later ones are often consequences (here b was never declared, so the next line fails too).

Tip: The block construct (block … end block) lets you open a new scope with its own declarations in the middle of the code. It is useful for small helper variables, as you'll see later.

Test yourself

Are Total and TOTAL the same variable in Fortran?

  1. Yes, names are case insensitive
  2. No, they are different
  3. Only inside modules
  4. Only if declared with implicit none

Which character continues a statement on the next line?

  1. \\
  2. ;
  3. &
  4. +

Where must variable declarations appear?

  1. Anywhere
  2. Before the first executable statement of the program unit
  3. After contains
  4. At the end of the program