Programming Fortran 1

About the author


Imperial College London

This tutorial is designed for those who have to learn Fortran. Not that Fortran is a bad language to learn and it is used extensively throughout the scientific community. However, it is often the first programming language scientists encounter, so this tutorial designed to give you the basics of the language, without (hopefully) the use of complicated paradigms or ideas.

A typical Fortran program, which was used to calculate the mass of the Earth, can be found by clicking here. This will open the program in a new window, so you can refer to it as you read the explanations.

Introduction

So what does this Fortran program do? We'll start by examining the code that all Fortran codes must use.

Basic stuff

  • Programs must start and end with:

    PROGRAM ProgName

    END PROGRAM ProgName
  • Each program must contain:
    IMPLICIT NONE
    This ensures that you have to declare every variable you use. If you don't, you may mistype a variable name and the compiler will not pick this up. This must go on the first programming line - i.e. before any variable declarations, but after the PROGRAM statement.
  • Comments are notes to you, the programmer, and anyone else that cares to read your work. In Fortran they are indicated by:
    ! This is a comment
    Comments should be used to explain the function of bits of code - e.g.:
    ! make sure units are correct, i.e. meters and kg per meter cubed
    radius = radius*(1000)
    density = density*(1000)

    which tells the reader that you are multiplying the variables by a thousand to correct for units, not because your algorithm needs it, our that you are stupid and don't know what you are doing. Comments are also useful to describe the function of the program and any subroutines. They should be concise, but in English, with good grammar.

  • To continue a statement on a new line, use the & character

    WRITE (11,*) "Earths mass, according to PEM model, is:" &
    ,SNGL(totalMass)," kg"

Variables

Variables are things that hold values. Values include numbers (1, 1.2242, 3.142), characters (a, b, silly) or true and false.

  • Variables are declared using:
    TYPE :: list of variable names
    The type can be one of:
    • Real - contain numbers with decimals
    • Integer - contain integer values
    • Character - contain alpha-numeric strings
    • Logical - contains .TRUE. or .FALSE.
  • Variable names should be descriptive, but not too verbose. They can contain any normal characters, but not spaces and they cannot start with a number. They also cannot be a Fortran keyword (IF, DO, END etc). Fortran variables are case insensitive, i.e. AvariaBLE is the same as aVariable.
  • Real variables can be normal real numbers or double precision. Double precision number can hold larger numbers, with more accuracy, but take up more memory. They are declared like:
    DOUBLE PRECISION :: aDoublePrecisionNumber
  • The length of the character string is determined using the LEN keyword
    CHARACTER(LEN=34) :: aString
    This will hold a string (i.e. letters), which is 34 or less characters, i.e.

    this string is 34 characters long

    If you had

    this string is 37ish characters long

    the o, n and g will be missed at the end

  • To declare a variable as an array:
    TYPE, DIMENSION(n, m, ...) :: list of variables with these dimensions
    An array is a list of numbers. This is a 1D array, with dimension statement DIMENSION(5)

    Element 1
    Element 2
    Element 3
    Element 4
    Element 5

    This is a 2D array, with dimension statement DIMENSION(5,3)

    Element 1,1 Element 1,2 Element 1,3
    Element 2,1 Element 2,2 Element 2,3
    Element 3,1 Element 3,2 Element 3,3
    Element 4,1 Element 4,2 Element 4,3
    Element 5,1 Element 5,2 Element 5,3
  • Note that the first axis, runs up-down. The second axis run left-right. All Fortran arrays start counting from 1, not 0 as in other languages
  • To declare a constant, something which does not change its value throughout the whole program, use:
    TYPE, PARAMTER :: constant = 2
  • You can do mathematical operations on the values contained in the variables. The operations are:
    • * - multiplication
    • + - addition
    • - - subtraction
    • / - division
    • ** - to the power of, 2 squared is 2**2

Dos and Ifs

DO loops are programming constructs that perform operations over and over again. IF constructs are how you make decisions in a piece of code, depending on the value of a variable.

  • Decisions are made in a program with IF...ELSE IF... END IF constructs

    IF (condition is true) THEN
    do some stuff
    ELSE IF (some other condition is true) THEN
    do some different stuff
    ELSE
    do the default stuff
    END IF

    You only need one IF...END IF to make the construct. The ELSE IF and ELSE are optional

  • The conditions must come out true or false. They are usually logical variables, which contain true or false, or conditions like:
    x < 5 - less than
    x == 4 - is equal to
    x > y - greater than
    x <= largeNumber - less than or equal to
    x /= 0 - not equal to
    x >= smallNumber - greater than or equal to
    One point to note is that an assignment, such as x = 5, always returns true. This means if you type x = 5 as your condition instead of x == 5, the IF code will always run. To get around this, if the thing your are comparing to is constant (either a number you actually type, or a variable you've declared as a parameter), then use:
    constant == x The compiler will now tell you if you typed
    constant = x
    by accident.
  • A do loop allows operations to be repeated many times. They need a variable to keep count of the number of iterations
  • A loop is set up with:
    DO i=1,10
    do some stuff
    END DO

    This will repeat the stuff inside the loop ten times. Each time, i, is incremented by one, until it reaches the value of ten.

  • You can change the stride of a loop with
    DO j=0,100,10
    stuff
    END DO

    which will skip 10 elements, i.e. will do j=0, j=10, j=20 etc, until j=100. The third number can be negative, allowing loops to run backwards

  • Both loops and if statements can be nested, allowing multiple paths to be taken or variables to be looped over:
    IF (I==0) THEN
    IF(j<0) THEN
    do some stuff
    END IF
    do some stuff
    ELSE
    do some different stuff
    END IF
  • DO i=1,5
    DO j=1,5
    do some stuff
    END DO
    END DO

    this will do the inner loop 100 times, before moving onto the second iteration of the outer loop. If you print out the values of i and j in each iteration (inside the inner loop) you'll get something like:

    i j
    1 1
    1 2
    1 3
    1 4
    1 5
    2 1
    2 2
    2 3
    2 4
  • Note the indentation in both cases. This makes the code easier to read

References

T.M.R Ellis et al., 1994. Fortran 90/95 Programming.