IMPLICIT NONE and implicit typing

Fortran can type a variable that nobody declared, and it decides the type from the first letter of the name. The rule is older than every other feature of the language and it is still on by default, which means a misspelled name does not stop the build: it creates a second variable. This lesson shows the rule, the bug it hides, and the one line that turns it off.

The letter rule

With implicit typing in force, the first letter of a name decides its type:

The rule comes from the arithmetic of the 1950s, where a loop counter was an integer and a measured value was a real, and the letters were chosen to match. A statement can change the mapping for a whole unit (implicit real(a-h, o-z) is the default written out), and nothing about it is checked against the way the name is used.

The typo it hides

The program below declares total, sets it, and then assigns to tota1, with a digit one where the letter l belongs. Nothing warns, because tota1 begins with t and is therefore a legal REAL. The program builds and prints the declared variable untouched.

The file implicit.f90:

program implicit integer :: total total = 3 tota1 = 4 print *, "total:", total print *, "tota1:", tota1 print *, "kind of tota1:", kind(tota1) end program implicit

gfortran -Wall -Wextra -std=f2018 implicit.f90 -o implicit
./implicit

total: 3
tota1: 4.00000000
kind of tota1: 4

Four bytes, as a real has on this machine, and the value prints with a decimal point, which is the visible sign of the second variable. The bug is quiet: the program runs, the output looks plausible, and the wrong name is only found by reading the source or by noticing that total never changed.

This is the failure mode that makes implicit typing a liability. A typo in a declared name is caught by the compiler; a typo in a name that was never declared is not, because the language has a rule for creating it.

IMPLICIT NONE

implicit none removes the rule for the program unit it appears in. Every name in that unit must then be declared, and an undeclared name is an error at the first line that uses it.

The file strict.f90:

program strict implicit none integer :: total total = 3 average = total / 2 print *, average end program strict

gfortran -Wall -Wextra -std=f2018 strict.f90 -o strict

strict.f90:6:3:

6 | average = total / 2
| 1~~~~~~
Error: Symbol 'average' at (1) has no IMPLICIT type

The message is the same one that greets every reader who adds the line to an old file, and it is good news: the compiler has found the mistake at the point where the name is first used. Declare average as a real, or as an integer if the division should truncate, and the build continues.

In every unit

implicit none applies to one program unit, not to a file and not to a module's users. A file with a module and a program needs the line twice, and so does every subroutine and function that is not inside a module with it.

This is the rule that a module makes cheap: put the line at the top of the module, and every procedure in its contains section inherits it. A module without the line leaves its procedures free to invent names, so the line belongs in the module header as much as in a program.

A build can also supply the line from outside. -fimplicit-none makes the compiler behave as if every unit began with it, which catches the units a reader forgot about:

gfortran -Wall -Wextra -std=f2018 -fimplicit-none implicit.f90 -o implicit

implicit.f90:5:3:

5 | tota1 = 4
| 1~~~~
Error: Symbol 'tota1' at (1) has no IMPLICIT type

A flag is a safety net, not a substitute: a program built with it and without the source line compiles only where the flag is passed, and the next reader who builds it by hand gets the old behaviour back. Write the line.

The full form

Fortran 2018 widened the statement. implicit none (type, external) also requires that every procedure called from the unit has an explicit interface, which means it must be in a module, be internal, or have an interface block. That closes the other half of the old hole: a call to a procedure nobody declared used to pass unchecked.

The file strict2.f90:

module strict_mod implicit none (type, external) contains subroutine show(x) real, intent(in) :: x print *, "x:", x end subroutine show end module strict_mod program strict2 use strict_mod, only: show implicit none (type, external) call show(2.5) end program strict2

gfortran -Wall -Wextra -std=f2018 strict2.f90 -o strict2
./strict2

x: 2.50000000

Both units carry the statement, and the call is checked because show comes from a module. A reader who writes new code can use this form from the start; a reader who has to keep an old compiler working stays with implicit none, which every compiler since Fortran 90 accepts.

Exercises

  1. Add implicit none to the program with the typo and rebuild. Which line does the compiler report first?
  2. Declare count and use cuont without implicit none. What type does cuont get?
  3. Write implicit real(a-z) at the top of a unit and declare an integer i below it. Does the build succeed?
  4. Build the typo program with -fimplicit-none and without the source line. Which line does the compiler report now?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which first letters make a name an INTEGER under implicit typing?

    Answer

    i to n

    The letters i, j, k, l, m and n map to INTEGER; the rest map to REAL.

  2. What does implicit typing do with an undeclared name?

    Answer

    It creates the variable with the type of its first letter

    That is the whole rule: no declaration, no message, a variable appears.

  3. What type does tota1 get without implicit none?

    Answer

    REAL

    The first letter is t, which maps to REAL under the default rule.

  4. What does implicit none apply to?

    Answer

    The program unit it appears in

    Each unit needs its own statement, or a module header that its procedures inherit.

  5. What error does an undeclared name give under implicit none?

    Answer

    Error: Symbol 'average' at (1) has no IMPLICIT type

    The message says the name has no implicit type, which is the rule being applied.

  6. What does -fimplicit-none do?

    Answer

    It makes the compiler treat every unit as if it began with implicit none

    The flag supplies the statement at compile time, without changing the source.

  7. What does this program print? program p integer :: total total = 3 tota1 = 4 print *, total end program p

    Answer

    3

    tota1 is a second, real variable, so total keeps its value of 3.

  8. What does implicit none (type, external) add?

    Answer

    Every called procedure must have an explicit interface too

    The external half requires an explicit interface for every procedure called.

  9. Where does implicit none go in a unit?

    Answer

    Before the declarations, right after the program or module statement

    It is a specification statement, so it belongs at the top of the specification part.

  10. Why is implicit typing more dangerous in a long program?

    Answer

    A typo in a name creates a second variable instead of stopping the build

    The compiler has a rule for the misspelled name, so the mistake stays silent.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev