Fortran

Fortran (FORmula TRANslation) appeared in 1957 as the first widely used high-level language and was built for numerical work. Modern Fortran (2003 and later) is fully structured, has modules and whole-array syntax, and still leads in weather, climate and physics codes.

Paradigm: Structured Programming

What Makes Fortran Special

  • Arrays are first-class. You can write sum((x - mean)**2) on a whole array without a loop. The compiler is free to vectorize and parallelize it.
  • Built for speed. Rules about aliasing let compilers optimize array code aggressively, so well-written Fortran matches or beats C on numeric kernels.
  • implicit none. Old Fortran guessed types from the first letter of a name. Modern code turns that off and declares every variable.
  • Modules and derived types. Modern Fortran groups code in modules and defines your own types, and even object-oriented features arrived in Fortran 2003.
  • Parallel by design. Coarrays, do concurrent and mature MPI and OpenMP support make it a natural fit for supercomputers.

Example: Mean and variance of an array

program stats
  implicit none
  real :: x(5) = [2.0, 4.0, 4.0, 4.0, 5.0]
  real :: mean, variance

  mean     = sum(x) / size(x)
  variance = sum((x - mean)**2) / size(x)

  print '(A, F6.2)', 'mean     = ', mean
  print '(A, F6.2)', 'variance = ', variance
end program stats

How It Works

  • implicit none forces every variable to be declared, and x(5) is an array of five reals initialized with an array constructor.
  • sum(x) and size(x) are intrinsic functions that operate on the whole array; no loop is written.
  • (x - mean)**2 creates a temporary array of squared differences in one expression.
  • The output is mean = 3.80 and variance = 0.96; the format string F6.2 prints six characters with two decimals.

History and Where It Is Used

Fortran dominates numerical weather prediction, climate models, computational chemistry, astrophysics and linear algebra libraries such as BLAS and LAPACK. Many Python and R scientific packages call Fortran underneath. See the Fortran roadmap for a complete course.

Learn More