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.
program welcome
implicit none
print '(a)', 'Hello from Fortran!'
print '(a, i0)', 'Fortran was first released in ', 1957
end program welcomeHello 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.
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_demoHeights (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 caseand the many forms ofdoloops. - 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 Intelifxcompiler. Then compile withgfortran hello.f90 -o helloand run./hello. Files with the.f90extension are treated as free-form source.
Test yourself
What does the name Fortran stand for?
- FORmal TRANsistor
- FORmula TRANslation
- FORward TRANsfer
- FOuR TRANsforms
Which standard introduced free-form source, modules and array syntax?
- FORTRAN 77
- Fortran 90
- Fortran 2003
- Fortran 2018
Why is Fortran still widely used in high-performance computing?
- It is the only language that runs on supercomputers
- Its array-centric design and strict aliasing rules let compilers optimise numeric code very well
- It has a garbage collector
- It is interpreted, so it starts faster