ZetCode

Fortran tutorials

Fortran is a compiled, statically typed language for numerical and scientific computing. It was created at IBM in the 1950s, which makes it the oldest high-level language still in everyday use, and it has been revised many times since: FORTRAN 66 and 77, Fortran 90, 95, 2003, 2008, 2018, and 2023. Modern Fortran has modules, derived types, object-oriented features, whole-array operations, and built-in parallel constructs, and it remains the language of many weather and climate models, physics codes, and numerical libraries.

GNU Fortran

The examples in these tutorials are compiled and run with gfortran, the Fortran front end of the GNU Compiler Collection (version 13.2). It is free, available on all major platforms, and supports the standards up to Fortran 2018 almost completely. The tutorials use free-form source in files with the extension .f90, write implicit none in every program, and are compiled with -std=f2018 -Wall -Wextra -fcheck=all. On Debian and Ubuntu the compiler is installed with the gfortran package.

Getting started

The introduction covers the history of the language and its standards, shows how to install gfortran, and compiles the first programs. The program structure article describes program units, the order of statements, free-form source, comments, continuation lines, and stopping a program.

Variables, types, and literals

The variables article explains declarations, why implicit none belongs in every program, named constants, the hidden save of an initial value, and scope. The data types and kinds article describes the five intrinsic types, the kind constants of iso_fortran_env, numeric limits, and conversions, and the literals article shows how values are written, including kind suffixes, BOZ constants, and the trap of single-precision real literals.

Expressions and control flow

The operators and expressions article covers arithmetic, relational, and logical operators, integer division, the two remainder functions, precedence, and the concatenation of strings. The conditionals article shows the if construct, else if chains, select case with ranges and characters, and merge. The loops article describes the counted do, do while, endless loops with exit, cycle, named loops, and the implied do, and the intrinsic numeric functions article collects the rounding, root, logarithm, and trigonometric functions that such code calls.

Procedures

The procedures article explains subroutines and functions, the intent attribute, the result clause, host association, keyword and optional arguments, and the interface blocks that let the compiler check a call. Procedures are the building blocks of every larger program, and the following articles collect them in modules and apply them to arrays, strings, and files.

Modules

The modules article collects constants and procedures in a compilation unit that other programs can use. It explains use with only and renaming, the public and private attributes, the .mod files that make separate compilation possible, and submodules, which keep an interface apart from its implementation.

Arrays

Fortran treats an array as a single value that can be assigned, sliced and passed as one argument. The arrays article covers declarations, constructors, sections and strides, whole-array arithmetic, column-major storage, and bounds checking. The array intrinsics article adds reductions, locations, masks, pack and where, the allocatable arrays article gives arrays a size chosen at run time, and the array arguments article shows how arrays and their shapes cross a procedure boundary.

Strings

A Fortran string has a length fixed when the variable is declared, and shorter values are padded with blanks. The strings article explains declarations, concatenation, substrings, trimming, searching and deferred-length variables, and the string processing article builds on them with case conversion, tokenizing, reversing, replacing and sorting text.

Input and output

Output is either list-directed, where the compiler chooses the layout, or formatted with edit descriptors that fix widths and decimals. The formatted output article covers the descriptors, tables and format statements, the internal files article uses the same statements to convert numbers to strings and back, and the reading input article reads numbers, lines and tables from the standard input and detects the end of the data.

Files and data

Data that outlives a program lives in files. The files article opens, reads, writes and appends text files and handles a failed open, the stream and binary files article covers unformatted records, stream access and direct access, and the namelists article offers a readable format for configuration. The CSV and text data article reads a table into arrays, computes statistics and writes a report.

Program design

A derived type groups related variables into a value of its own, with components, defaults and structure constructors; arrays and allocatable strings can be components too. A type can then own its procedures, inherit from another type, take part in polymorphic calls and overload operators. Pointers connect the values of a type to other values, which is how a list, a stack, a queue or a tree is built without an array.

The rest of the section puts those building blocks to work on classic programming problems. Recursion, sorting and searching are written by hand so that the cost of each method is visible, and procedure arguments let one routine work with many different callbacks.

Numbers and precision

The number types deserve a section of their own. A complex value is a pair of reals with a type of its own, an integer exposes the bits it is made of, and a real obeys the IEEE rules for rounding, NaN and infinity, which a program can test and control. When the hardware limits are too small, big integers built from arrays of digits compute the exact answer, and random numbers, reproducible from a seed, drive simulations and sampling.

Tools and runtime

A program that is meant to be used rather than read needs more than arithmetic. It reads the command line for its options, looks up an environment variable or starts another program, reports its failures through status codes and error stop, and stamps its output with the date and time taken from the system clock.

Numerical computing

The reason Fortran survives is the work it was invented for. Numerical integration and root finding turn a function into a number, linear algebra solves systems of equations with the built-in array operations and with LAPACK, and scientific simulations step a model of the world forward in time while checking the quantities that must not change.

Parallel computing

Most processors now have several cores, and Fortran has three ways to use them. Parallel loops explain the do concurrent construct and what a compiler does with it, OpenMP adds directives for threads that share memory, and coarrays spread a program over several images that exchange data by naming each other's copies.

Interoperability and legacy code

A Fortran program rarely stands alone. C interoperability passes numbers, strings, arrays, structures and callbacks between the two languages, and the FORTRAN 77 article reads and modernises the fixed-form code that still fills numerical libraries.

Building and maintaining

The last three articles are about the working habits around the language. Compiling and debugging covers the compiler flags, gdb, separate compilation and make, unit testing builds a harness and a suite with tolerances and exit statuses, and the style guide collects the rules that keep a program readable and checkable.

History and standards

Fortran has been standardized eight times since 1966, and the language has kept almost all of what each revision added. Two articles close the series with that history: What is new in Fortran 2018 and 2023 takes the last two revisions apart feature by feature and records which of them the installed compiler accepts, and Fortran through the standards writes one small program five times to show what changed between FORTRAN 66, FORTRAN 77, Fortran 90, Fortran 2003 and Fortran 2018.

Language features

Fortran is built around arrays: whole arrays can be added, multiplied, sliced, and passed to procedures as single values, and the compiler optimises them for speed. Numbers come in several kinds, so a program states the precision it needs and gets the same result on every compiler. Procedures are collected in modules, which give the compiler the information it needs to check every call. Formatted input and output is done with edit descriptors, and files are connected to numbered units.

These tutorials introduce the language one topic at a time. Each article is self-contained, and every example is compiled and run with GNU Fortran 13.2.0 before its output is shown.

last modified November 14, 2026