Professional Documents
Culture Documents
FORTRAN 90 Basics
This document provides only a very basic introduction to FORTRAN 90 to allow you to begin
writing FORTRAN 90 programs. FORTRAN 90 is actually a very powerful language with many
features not described here. For a readable full description of the language see, for example,
FORTRAN 90/95 explained, by Metcalf and Reid (Oxford University Press).
A FORTRAN program consists of a sequence of lines of text called statements, which are
instructions to the computer. The outline structure of a program is as follows:
PROGRAM name
Type declaration statements
Executable statements
END PROGRAM name
Here items in italics are not to be taken literally; they indicate the sort of thing that should appear
in that position. Executable statements are statements that do things, for example calculate an
expression, read or write to a file, etc. Type declaration statements tell the compiler about the
types of data the program will use, etc.
1.2. Comments
Anything to the right of a "!" is regarded as a comment and is ignored by the compiler (except
when the "!" appears within a character string). E.g.
! This is a comment line
x = x + 1 ! and this statement includes a comment
You are strongly encouraged to use comments. A few seconds typing in comments can save hours
or days of head scratching when you or someone else tries to understand the code later.
1
MTMA33 FORTRAN BASICS 2007
2.1. Types
FORTRAN can manipulate several different types of data
Each type of data can come in several kinds, for example with higher precision. The most useful of
these is DOUBLE PRECISION, which is like a REAL but with more significant figures stored and a
wider range of exponents allowed. FORTRAN_90 also allows the programmer to define their own
types of data, for example a type consisting of an integer and a character string that could contain
a code number and a name.
2
MTMA33 FORTRAN BASICS 2007
2.3. Variables
Variable names must have between 1 and 31 alphanumeric characters (letters, underscores and
numerals), and the first must be a letter. Choosing variable names sensibly will greatly help the
intelligibility of your code. Variables can have any of the types discussed in section 2.1. The type of
each variable can be declared in the type declaration statements at the start of the program. E.g.
INTEGER :: i, j
REAL :: heat_capacity
The length of a character variable is specified in the type declaration.
CHARACTER(LEN=20) :: axis_title
By default, variables whose type is not specified are assumed, implicitly, to be INTEGER if their
name starts with i, j, k, l, m, or n and REAL otherwise. However, you are strongly encouraged to
override this by inserting the statement
IMPLICIT NONE
before your type declaration statements. Then all variables must be declared; any not declared will
cause an error message. Doing this speeds up program development enormously by catching a lot
of bugs that would otherwise be difficult to track down.
2.5. Parameters
A parameter is a named constant. It cannot be changed by the program as it runs. However, the
programmer can change it between runs. Parameters are useful when the same constant appears
in many places in a program, for example as an array size. A parameter type is declared and its
value assigned in a type statement. E.g.,
INTEGER, PARAMETER :: npoints = 1000
REAL, PARAMETER :: pi = 3.14159
3
MTMA33 FORTRAN BASICS 2007
3. Calculations
3.1. Assignment
Assignment statements are of the form
variable = expression
This means evaluate the expression on the right hand side and put the result into the variable
appearing on the left hand side. Any value that the variable had previously is overwritten and lost.
The "=" sign thus has a slightly different meaning from its usual mathematical meaning. For
example,
x = x + 1.0
is a perfectly valid assignment statement and involves no contradiction.
Unless it has been given an initial value in a type or data statement, a variable is undefined, i.e.
has no definite value, until it appears on the left hand side of an assignment statement or is read
in a READ statement. Then it becomes defined. Undefined variables cannot be used in expressions.
4
MTMA33 FORTRAN BASICS 2007
For example, if a and b are real variables equal to 1.0 and 2.0 respectively, then a < b would have
the value .TRUE. while a == b would have the value .FALSE. .
E.g.,
fun = sun .and. surf ! fun, sun and surf
! are LOGICAL variables
5
MTMA33 FORTRAN BASICS 2007
6
MTMA33 FORTRAN BASICS 2007
4.1. Printing
To print results to the monitor a PRINT statement is used.
PRINT *, expression1, expression2, ...
The expressions expression1, expression2, ... can be of any type.
4.2. Reading
To read data from the keyboard a READ statement is used.
READ *, variable1, variable2, ...
When the program reaches the READ statement it pauses and waits for the data to be typed in.
Note that it is advisable to have a PRINT statement before the READ statement otherwise you
won’t know that the programme is waiting for you to enter something!
E.g.,
PRINT *, 'Enter two REAL numbers'
READ *, a, b
PRINT *, 'Their sum is ', a + b
5. Control statements
Normally FORTRAN statements are executed in top-to-bottom order. The program stops when it
reaches the END PROGRAM statement. (The program can be made to stop elsewhere by a STOP
statement.) Control statements allow the program to depart from the normal top-to-bottom
exectution order. Control statements are what give computer programs the power to perform very
complex calculations.
7
MTMA33 FORTRAN BASICS 2007
8
MTMA33 FORTRAN BASICS 2007
The loop is entered or re-entered only if logical_expression is .TRUE. . This form of the construct is
useful when the number of iterations is not known before entering the loop. Obviously the
calculations carried out by statements must result in the logical_expression eventually becoming
.FALSE., otherwise the loop will carry on for ever.
Like IF constructs, DO loops can be given names to improve the readability of complex codes and
to help the compiler's error checking. E.g.,
timeloop: DO step = 1, nstop
statements
ENDDO timeloop
DO loops can be nested one within another. Also DO loops can contain IF constructs and IF
constructs can contain DO loops.
9
MTMA33 FORTRAN BASICS 2007
6. Arrays
Arrays are a way of giving lots of data values of the same type the same name, allowing lots of
similar calculations to be done with a small amount of code. Arrays may be of any type, and the
rules for naming them are the same as for non-array variables (called scalars). The elements of an
array are identified by indices. The number of indices needed is the dimension of the array, which
may range from 1 to 7.
FORTRAN 90 can also perform calculations on whole arrays using a single statement. For example
a = 2.0*b ! a and b are arrays
takes each element of the array b, multiplies it by 2.0, and stores the result in the corresponding
element of array a. The arrays a and b must be conformable, i.e. have the same shape and size.
Subarrays may also be manipulated by specifying a range of indices. E.g.,
a(1:10) = c(11:20,i) + d(n+1:n+10) + 1.0
PRINT *,' a = ', a(1:10)
Again, the subarrays must be conformable. Note that a scalar is conformable with any array, in the
obvious way.
10
MTMA33 FORTRAN BASICS 2007
7. Subprograms
A piece of code that needs to be executed at several different places in a program can be put in a
subprogram and that subprogram called from the main program at the appropriate places. This
reduces program length and programmer effort, and reduces the risk of introducing bugs. The use
of subprograms also produces more modular and structured code, and so you are encouraged to
place well-defined subtasks in subprograms even if they are used only once. FORTRAN allows two
kinds of subprogram: SUBROUTINEs and FUNCTIONs
7.1. Subroutines
A SUBROUTINE has a very similar structure to a PROGRAM, except that it can include some
dummy arguments.
The dummy arguments provide one way to pass data between the main program and the
subroutine. (Another way is through modules.) A statement of the form
CALL name(actual1, actual2, ...)
has the effect of executing the subroutine statements with the dummy arguments replaced by the
corresponding actual arguments. See Example 10. The actual arguments may be constants or
variables, including arrays. The dummy arguments must be variables. Usually the CALL and
SUBROUTINE statements should have the same number of arguments, and the corresponding
argument should have the same type and shape and size.
When an argument is an array it is often convenient to pass its shape and size information as
arguments too. See Example 11.
Some arguments may be intended solely for passing data into the subroutine and should not be
modified by the subroutine. Others may be intended solely for passing results out of the
subroutine and need not be defined on the way in. Yet others may be intended for passing data
both ways. The clarity of the code may be improved and the compiler's error checking helped by
specifiying the intent in the subroutine's type declaration statements. See Example 10 and
Example 11. The INTENT may be IN, OUT, or INOUT.
SUBROUTINEs may CALL other SUBROUTINEs. However, special steps must be taken if a
SUBROUTINE is required to call itself, either directly or indirectly.
11
MTMA33 FORTRAN BASICS 2007
7.2. Functions
A FUNCTION has a similar structure to a SUBROUTINE.
type FUNCTION name(dummy1, dummy2, ...)
Type declaration statements
Executable statements
END FUNCTION name
The important differences are that a FUNCTION has a type, and that somewhere in the executable
statements name must be assigned a value. It is poor programming practice to change the values
of the dummy arguments within a FUNCTION. If you need to do this then it's better to use a
SUBROUTINE. A FUNCTION is invoked by using its name in a similar way to using any intrinsic
function. See Example 8.
12
MTMA33 FORTRAN BASICS 2007
8. Files
Data may be be written to or read from files stored on the computers discs. A file may be opened
for reading or writing using an OPEN statement.
OPEN(unit, file = filename)
Here unit is an INTEGER expression (usually in the range 1-99) and filename is a CHARACTER
expression. Subsequent READ or WRITE statements then refer to the file via the unit number.
READ(unit1, format1) inputlist
WRITE(unit2, format2) outputlist
Here inputlist is a list of variables to be read in from the file, and outputlist is a list of variables
and constants to be written out to the file.
There are various ways of specifying the format of the data to be read or written, depending on
the format parameter of the READ or WRITE statement. The simplest case is when the format
parameter is an asterisk (list-directed I/O). Then a READ statement does its best to read the data
whatever format it is in, and a WRITE statement choses a reasonable format for the data being
written at that moment.
The user can control the format explicitly by specifying it in a CHARACTER string built up from
edit descriptors separated by commas and enclosed in parentheses.
Longer format strings, or format strings that are to be used repeatedly, can be specified in a
FORMAT statement and referred to by a label. E.g.,
WRITE(6,900) a(1:100)
900 FORMAT(10F12.5)
In this example the FORMAT statement specifies 10 numbers to be written, each in floating point
format, and each with 12 characters including leading blanks, and 5 decimal places. The WRITE
statement writes 100 numbers, so the specified format is recycled 10 times, producing 10 lines of
output, each with 10 numbers.
13