Anatomy of a first program

Here is a complete Fortran program in free source form:

program hello implicit none print *, 'Hello, world!' end program hello

Four parts make up the skeleton. The program statement names the program. Declarations describe the variables it will use (this example has none). Statements do the work — here, print * writes to standard output. And end program marks the end. The comment marker in free form is an exclamation mark; everything after one on a line is ignored.

IMPLICIT NONE and why it is recommended

Fortran has a historical rule called implicit typing: a variable whose name begins with certain letters is assumed to be integer, and others are assumed to be real, unless declared otherwise. A misspelled variable name then quietly becomes a new variable with a default type — a classic source of bugs. The statement implicit none turns the rule off, so every variable must be declared. It is standard practice to put it near the top of every program and module.

Basic data types

The core types are integer for whole numbers, real for floating-point numbers, character for text, and logical for true/false values. Declarations look like this:

integer :: count real :: temperature character(len=20) :: name logical :: done

Each type comes in kinds — integer and real kinds of different precision are selected with a kind parameter, and the chosen kind sets range and accuracy.

Compiling and running in general terms

A program is written in a source file, conventionally with a .f90 suffix for free-form code. A Fortran compiler translates the source into an executable file, which is then run from the command line. The exact commands depend on the compiler and operating system; this site covers the language itself and leaves tool-specific instructions to each compiler's documentation.

With the skeleton, the types, and a first program in hand, the natural next step is Arrays and Intrinsic Functions, where the language starts to show its strengths.