Fortran Modern alternatives to historical features Implicit variable types


When Fortran was originally developed memory was at a premium. Variables and procedure names could have a maximum of 6 characters, and variables were often implicitly typed. This means that the first letter of the variable name determines its type.

  • variables beginning with i, j, ..., n are integer
  • everything else (a, b, ..., h, and o, p, ..., z) are real

Programs like the following are acceptable Fortran:

program badbadnotgood
  j = 4
  key = 5 ! only the first letter determines the type
  x = 3.142
  print*, "j = ", j, "key = ", key, "x = ", x
end program badbadnotgood

You may even define your own implicit rules with the implicit statement:

! all variables are real by default 
implicit real (a-z)


! variables starting with x, y, z are complex
! variables starting with c, s are character with length of 4 bytes
! and all other letters have their default implicit type
implicit complex (x,y,z), character*4 (c,s) 

Implicit typing is no longer considered best practice. It is very easy to make a mistake using implicit typing, as typos can go unnoticed, e.g.

program oops
  real :: somelongandcomplicatedname


  call expensive_subroutine(somelongandcomplEcatedname)
end program oops

This program will happily run and do the wrong thing.

To turn off implicit typing, the implicit none statement can be used.

program much_better
  implicit none
  integer :: j = 4
  real :: x = 3.142
  print*, "j = ", j, "x = ", x
end program much_better

If we had used implicit none in the program oops above, the compiler would have noticed immediately, and produced an error.