What Fortran is and where it runs

Fortran is a compiled language for numerical work. It has been in continuous use since 1957, it still runs the weather models, the finite element codes and the linear algebra libraries that other languages call, and its modern form is a small, strict language with arrays as first-class values. This lesson says what the language is, which standards matter, and what a first program looks like.

What Fortran is

A Fortran program is a set of program units: one main program, and any number of modules, subroutines and functions. The source is compiled ahead of time into a native binary, the same way C is, so a Fortran program starts without a runtime and links against the same libraries as the C code around it.

Three properties separate it from the languages next to it. Arrays are values: a = b + c adds two arrays element by element, and the compiler knows their shape. Numeric types carry a kind, so a program states its precision instead of hoping. And the language has no pointers in the ordinary sense, no garbage collector and no header files: a module is compiled once, and its interface is stored in a small file the compiler reads.

Standards and versions

Fortran is standardised, and every compiler states which standard it takes. The names below are the ones a reader meets in a build script or a paper, and the version a course uses is the one its compiler supports.

Where Fortran is used

Numerical libraries are the centre of gravity: BLAS and LAPACK for linear algebra, the netCDF and HDF5 readers of the climate and weather world, and the solvers inside engineering codes. A large part of it is not written from scratch any more: a Python or C++ program calls a Fortran library, and the Fortran part is the part that does the arithmetic.

The language is a good fit when the work is a loop over a large array, when the compiler must be able to reorder that loop, and when the precision has to be stated. It is a poor fit for a command line tool, a web service or a program that spends its time on strings and hash tables, and the last article of the Fortran and C++ group says why in both directions.

A first program

The program below prints one line. program hello opens the main unit and end program hello closes it; implicit none turns off the old rule that guessed the type of an undeclared name; and print * writes to standard output in list-directed form, which is the shortest way to print a value.

program hello implicit none print *, "Hello, Fortran!" end program hello

Save it as hello.f90 and build it. The extension matters: .f90 is read as free-form source, .f as fixed form. The command states the standard and the two warning levels this course uses on every page.

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

Hello, Fortran!

The space before the text is not a typo: list-directed output starts every record with a blank, a rule that goes back to the line printers of the 1960s and that the standard still keeps.

A program unit can also be written without a name (end instead of end program hello), and the compiler accepts it. The named form is worth the five extra words: the compiler reports errors against the unit, and a reader sees where the file starts.

The mistake it refuses

Every name in a unit with implicit none must be declared. A typo therefore stops the build instead of creating a second variable that quietly holds a wrong value. The program below declares total and then assigns totol.

program typo implicit none integer :: total totol = 1 print *, total end program typo

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

typo.f90:6:3:

6 | totol = 1
| 1~~~~
Error: Symbol 'totol' at (1) has no IMPLICIT type; did you mean 'total'?

Without implicit none the same line would compile: the first letter t would make totol a REAL, the value would be a real zero, and the program would print the INTEGER total untouched. That rule is the subject of the lesson on IMPLICIT NONE.

Exercises

  1. Change the greeting and rebuild. What changes in the output, and what does not?
  2. Remove implicit none from the broken program and compile it. What does the program print, and why is that worse than an error?
  3. Replace print * with write (*, *). Which form does the compiler prefer with -Wall?

Questions

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

  1. What does the standard name Fortran 2018 add over Fortran 2008?

    Answer

    SELECT RANK and better C interoperability

    Fortran 2008 brought coarrays; 2018 added SELECT RANK, the C descriptors and the collective subroutines.

  2. Why is a file named hello.f90 read as free form?

    Answer

    Because gfortran reads .f90 as free form and .f as fixed form

    The extension selects the source form, and .f90 is the free-form one.

  3. What does print *, "Hello" write, exactly?

    Answer

    A blank, then Hello

    List-directed output starts each record with a blank.

  4. What is a kind in Fortran?

    Answer

    A number that selects a precision or range of a type

    REAL(real64) and INTEGER(int32) name a precision with a kind value.

  5. What does this program print? program p implicit none integer :: total total = 3 print *, total end program p

    Answer

    3

    List-directed output writes a leading blank, then the value 3.

  6. What is the first error line of this program? program p implicit none integer :: total totol = 1 print *, total end program p

    Answer

    Error: Symbol 'totol' at (1) has no IMPLICIT type; did you mean 'total'?

    The name is not declared and implicit none forbids guessing its type.

  7. Which program unit holds the main program of a Fortran file?

    Answer

    The unit that starts with PROGRAM

    A PROGRAM statement opens the main unit; modules and procedures are the others.

  8. What is stored in a .mod file?

    Answer

    The interface of a module, for the compiler

    gfortran writes a .mod file with the interface so a user compiles against it.

  9. What does this program print? program p implicit none print *, "area:", 2 * 3 end program p

    Answer

    area: 6

    List-directed output pads an integer to its field width, so the 6 sits far to the right.

  10. Why does this course build with -std=f2018 rather than -std=legacy?

    Answer

    f2018 rejects the obsolete forms that legacy keeps, so a page cannot drift into old syntax

    The standard flag decides what the compiler accepts, and the course wants the modern set.

The code on this page was 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