TExcelTAD
Home › Fortran › Fortran implicit none

Fortran implicit none

Early Fortran did not require declarations. Instead, it used implicit typing: any undeclared name starting with the letters I to N was an integer, and every other name was a real. (Mathematicians traditionally used i, j, k, l, m, n for indices, so the rule felt natural in 1957.)

That rule still exists today for backwards compatibility. The statement implicit none switches it off, so every variable must be declared. Write it in every program, module and procedure you create.

Why implicit typing is dangerous

Without implicit none, a typo silently creates a brand-new variable:

fortran · try it yourself
program typo_bug
  ! no implicit none here!
  total = 0.0
  do i = 1, 5
    totl = total + i      ! typo: 'totl' instead of 'total'
  end do
  print *, 'Total is', total
end program typo_bug
 Total is   0.00000000    

The program compiles and runs, but the answer is wrong: the loop keeps assigning to a new real variable totl, and total never changes. In a large simulation, a bug like this can go unnoticed for months.

Add implicit none and the compiler catches it immediately:

fortran · try it yourself
program typo_caught
  implicit none
  real :: total
  integer :: i
  total = 0.0
  do i = 1, 5
    totl = total + i
  end do
  print *, 'Total is', total
end program typo_caught
/app/example.f90:7:5:

    7 |     totl = total + i
      |     1~~~
Error: Symbol 'totl' at (1) has no IMPLICIT type; did you mean 'total'?

gfortran even suggests the name you probably meant. Fix the typo and it works:

fortran · try it yourself
program typo_fixed
  implicit none
  real :: total
  integer :: i
  total = 0.0
  do i = 1, 5
    total = total + i
  end do
  print *, 'Total is', total
end program typo_fixed
 Total is   15.0000000    

The integer-name trap

Implicit typing has a second, subtler problem: a variable starting with i–n is an integer, so fractions vanish.

fortran · try it yourself
program mass_trap
  ! no implicit none
  mass = 2.75                 ! 'mass' starts with m -> integer!
  velocity = 3.0              ! 'velocity' -> real
  energy = 0.5 * mass * velocity**2
  print *, 'mass =', mass
  print *, 'energy =', energy
end program mass_trap
 mass =           2
 energy =   9.00000000    

The correct kinetic energy is 12.375, but mass silently became the integer 2.

Where to write it

implicit none goes right after any use statements and before the declarations, in every program unit:

fortran
module geometry
  implicit none          ! covers the module AND every procedure inside it
contains
  real function area(r)
    real, intent(in) :: r
    area = 3.14159265 * r**2
  end function area
end module geometry

program main
  use geometry
  implicit none          ! needed again: the main program is a separate unit
  print *, area(1.0)
end program main

A single implicit none in a module also applies to the procedures after its contains, so you don't need to repeat it there (repeating it does no harm).

Tip: gfortran has a flag -fimplicit-none that acts as if every unit started with implicit none. It's a good safety net, but still write the statement: the code then behaves the same on every compiler.

Test yourself

Without implicit none, what type does an undeclared variable named count get?

  1. integer
  2. real
  3. character
  4. logical

What is the main benefit of implicit none?

  1. Faster programs
  2. Typos and undeclared variables become compile errors
  3. Variables are initialised to zero
  4. It enables object-oriented features

Where does implicit none go?

  1. At the very end of the program
  2. After use statements, before declarations
  3. Before the program statement
  4. Inside each do loop