230 Contributors: 14 Friday, June 16, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial:
Roadmap: roadmap

Getting started with Fortran


Fortran is a language used extensively in the scientific community due to its suitability for numerical computation. Particularly attractive is its intuitive array notation, which makes writing fast vectorised computations easy.

Despite its age, Fortran is still actively developed, with numerous implementations, including GNU, Intel, PGI and Cray.


FORTRAN 66First standardization by ASA (now ANSI)1966-03-07
FORTRAN 77Fixed Form, Historic1978-04-15
Fortran 90Free Form, ISO Standard, Array operations1991-06-15
Fortran 95Pure and Elemental Procedures1997-06-15
Fortran 2003Object Oriented Programming2004-04-04
Fortran 2008Co-Arrays2010-09-10

Case insensitivity

Uppercase and lowercase letters of the alphabet are equivalent in the Fortran character set. In other words, Fortran is case insensitive. This behavior is in contrast with case-sensitive languages, such as C++ and many others.

As a consequence, the variables a and A are the same variable. In principle one could write a program as follows

pROgrAm MYproGRaM
enD mYPrOgrAM

It's to the good programmer to avoid such ugly choices.

Hello, world

Any Fortran program has to include end as last statement. Therefore, the simplest Fortran program looks like this:


Here are some examples of "hello, world" programs:

print *, "Hello, world" 

With write statement:

write(*,*) "Hello, world"

For clarity it is now common to use the program statement to start a program and give it a name. The end statement can then refer to this name to make it obvious what it is referring to, and let the compiler check the code for correctness. Further, all Fortran programs should include an implicit none statement. Thus, a minimal Fortran program actually should look as follows:

program hello
  implicit none
  write(*,*) 'Hello world!'
end program hello

The next logical step from this point is how to see the result of the hello world program. This section shows how to achieve that in a linux like environment. We assume that you have some basic notions of shell commands, mainly you know how to get to the shell terminal. We also assume that you have already setup your fortran environment. Using your preferred text editor (notepad, notepad++, vi, vim, emacs, gedit, kate, etc.), save the hello program above (copy and paste) in a file named hello.f90 in your home directory. hello.f90 is your source file. Then go to the command line and navigate to the directory(home directory?) where you saved your source file, then type the following command:

>gfortran -o hello hello.f90

You just created your hello world executable program. In technical terms, you just compiled your program. To run it, type the following command:


You should see the following line printed on your shell terminal.

> Hello world!

Congratulations, you just wrote, compiled and ran the "Hello World" program.

Installation or Setup

Fortran is a language which can be compiled using compilers supplied by many vendors. Different compilers are available for different hardware platforms and operating systems. Some compilers are free software, some can be used free of charge and some require the purchase of a licence.

The most common free Fortran compiler is GNU Fortran or gfortran. The source code is available from GNU as a part of GCC, the GNU compiler collection. Binaries for many operating systems are available at Linux distributions often contain gfortran in their package manager.

Further compilers are available for example:

On HPC-Systems there are often specialized compilers available by the system provider as for example the IBM or Cray compilers.

All these compilers support the Fortran 95 standard. An overview on the Fortran 2003 status and the Fortran 2008 status by various compilers is offered by the ACM Fortran Forum and available in the Fortran Wiki.

Quadratic equation

Today Fortran is mainly used for numerical computation. This very simple example illustrates the basic program structure to solve quadratic equations:

program quadratic
  !a comment

  !should be present in every separate program unit
  implicit none

  real :: a, b, c
  real :: discriminant
  real :: x1, x2

  print *, "Enter the quadratic equation coefficients a, b and c:"
  read *, a, b, c

  discriminant = b**2 - 4*a*c

  if ( discriminant>0 ) then

    x1 = ( -b + sqrt(discriminant)) / (2 * a)
    x2 = ( -b - sqrt(discriminant)) / (2 * a)
    print *, "Real roots:"
    print *, x1, x2

    ! Comparison of floating point numbers for equality is often not recommended. 
    ! Here, it serves the purpose of illustrating the "else if" construct. 
  else if ( discriminant==0 ) then

    x1 = - b / (2 * a)
    print *, "Real root:"
    print *, x1

    print *, "No real roots."
  end if
end program quadratic