TExcelTAD
Home › Fortran › Fortran Intro

Fortran Intro

Fortran (from FORmula TRANslation) is the oldest high-level programming language still in everyday use. It was designed at IBM by a team led by John Backus and released in 1957 for the IBM 704. Its goal was simple and radical for the time: let scientists write mathematical formulas in a readable form and have a compiler turn them into machine code that was almost as fast as hand-written assembly.

Nearly seventy years later, Fortran still powers weather forecasts, climate models, computational fluid dynamics, nuclear and particle physics, quantum chemistry and many of the benchmarks used to rank the world's supercomputers. Libraries such as BLAS and LAPACK, which sit underneath NumPy, MATLAB and R, were written in Fortran.

fortran · try it yourself
program welcome
  implicit none
  print '(a)', 'Hello from Fortran!'
  print '(a, i0)', 'Fortran was first released in ', 1957
end program welcome
Hello from Fortran!
Fortran was first released in 1957

Modern Fortran is not your grandparents' Fortran

Old programs were written for punched cards: upper-case only, statements in columns 7–72, GOTO everywhere. If you have seen code like that, forget it. The language has been revised many times:

Standard Highlights
FORTRAN 66, 77 first standards; structured IF/ELSE, CHARACTER type
Fortran 90 free-form source, modules, array syntax, allocatable arrays, derived types, implicit none
Fortran 95 pure and elemental procedures, forall
Fortran 2003 object-oriented programming, C interoperability, allocatable improvements
Fortran 2008 coarrays (built-in parallelism), do concurrent, submodules
Fortran 2018 more parallel features, better C interop, error-handling improvements
Fortran 2023 conditional expressions, enumeration types, small conveniences

This course teaches modern free-form Fortran (2008/2018 style), the kind written in new projects today.

Why Fortran is still fast

Fortran was designed around arrays of numbers. The compiler knows a great deal about your data: arrays are a built-in type, two dummy arguments are not allowed to overlap in memory unless you say so, and many functions are marked side-effect-free. This lets optimisers vectorise and parallelise loops aggressively.

fortran · try it yourself
program vector_demo
  implicit none
  real :: heights(5) = [1.62, 1.75, 1.80, 1.68, 1.91]

  ! Whole-array arithmetic: no loop needed
  print '(a, 5f6.2)', 'Heights (m):  ', heights
  print '(a, 5f6.1)', 'Heights (cm): ', heights * 100.0
  print '(a, f6.3)',  'Average (m):  ', sum(heights) / size(heights)
end program vector_demo
Heights (m):    1.62  1.75  1.80  1.68  1.91
Heights (cm):  162.0 175.0 180.0 168.0 191.0
Average (m):   1.752

What you will learn

  • The basics: program structure, variables, kinds (precision), operators and formatted output.
  • Control flow: if, select case and the many forms of do loops.
  • Arrays, Fortran's superpower: slicing, whole-array operations, intrinsic reductions, matrices and allocatable arrays.
  • Procedures and modules, derived types, generic interfaces, operator overloading and object-oriented Fortran.
  • Scientific programming: numerical integration, root finding, linear systems, C interoperability and parallelism.

How the examples run here

Every fortran run example is sent to Compiler Explorer, compiled with gfortran 15.2 (the GNU Fortran compiler) and executed; the output appears under the code. You can edit any example and run it again. Programs cannot read from the keyboard, so input values are written into the code.

Note: To work on your own machine, install gfortran (part of GCC; sudo apt install gfortran, brew install gcc, or MSYS2 on Windows), or the Intel ifx compiler. Then compile with gfortran hello.f90 -o hello and run ./hello. Files with the .f90 extension are treated as free-form source.

Test yourself

What does the name Fortran stand for?

  1. FORmal TRANsistor
  2. FORmula TRANslation
  3. FORward TRANsfer
  4. FOuR TRANsforms

Which standard introduced free-form source, modules and array syntax?

  1. FORTRAN 77
  2. Fortran 90
  3. Fortran 2003
  4. Fortran 2018

Why is Fortran still widely used in high-performance computing?

  1. It is the only language that runs on supercomputers
  2. Its array-centric design and strict aliasing rules let compilers optimise numeric code very well
  3. It has a garbage collector
  4. It is interpreted, so it starts faster