Skip to main content

1. Introduction

Code format

The basic layout of an F90 program where the modules and functions are internal is as follows

module1 
module2 
program name

implicit none
constant/variable declarations
parameter declarations
data declarations

use module1
use module2

body of program

contains

function1
function2

end program name


The general structure of a module (called module_name in this example) is as follows

module module_name (parameters)

local variable declarations
contains

body of function code

end module module_name

The structure of a function is very similar, but doesn't use contains - just replace "module" with "function".

A first code

Putting it into Practice:

Here is a trivial first code that includes some output. Using emacs, open a file called hello.f90, and put the following into this file. Then save and quit emacs.
PROGRAM HELLO
IMPLICIT NONE
! write out "Hello World" to your x-term
   PRINT *, "Hello World!"
END PROGRAM HELLO
Now you should compile this code. At the prompt, type
ifort hello.f90 -o hello

Note: ifort is the Intel Fortran compiler. Another common Fortran compiler is called gfortran (from GNU).

The program should have compiled without error, and made an executable called hello. This executable is now ready to run. To run it, type at the prompt (and then hit return)

./hello
On your x-term, you should see the output Hello, World!

Notes on your first code

  • The first line starts the program, and gives a name to this program - in this case the name is HELLO.
  • The second line is important - it means that you have to declare each variable/constant explicitly. Neglecting to include this line can give you problems when it comes to debugging!
  • Comment statements are very important in all code. In F90, comment statements are prefixed by an exclamation mark (!). Get into the habit of writing helpful comment statements in your code.
  • Each statement line in F90 is confined to 132 characters in length. To continue a line, use an ampersand (&) at the end of the old line, with the new line appearing directly underneath. The exception is when splitting a string (as seen in the example above) where you also need to prefix the 2nd line with an ampersand.
  • You can include several statements on one line. Each statement on the line must be separated by a semicolon (;)