HANDLEIDING FORTRAN

APPENDIX B: FORTRAN Course


B.1. Introduction to Computers

Information to be stored or processed in a computer must be coded as strings of 0's and 1's (i.e. in binary form).

It is possible to code in binary form whole numbers (integers), whether positive or negative. There is a limit to the size of a whole number which can be stored in any computer.

Numbers which are usually written with a decimal point may also be coded in binary form. This is done by storing the number in two parts, exponent and the fraction. Such numbers may be positive or negative. They are called real or floating point numbers. There are limits to both the size and accuracy of floating point numbers when stored in any computer.

Characters may also be coded in binary form by using a certain pattern of bits to represent a particular character.

In order to decode information stored in a computer it is necessary to know what convention was used to code it.

Instructions are stored in binary form in a computer. They are normally obeyed one after the other in sequence, but some instructions may cause control to be passed to another point inside the computer. Programs (sets of instructions) may therefore contain loops, i.e. parts of the program which are obeyed several times over.

B.2. Introduction to FORTRAN

A program written in FORTRAN must be translated into machine code instructions by means of a special program called a compiler. Then the translated program can be executed by the computer.

A FORTRAN variable is a way of referring to a cell of the computer. Names for variables must conform to the following rules:

  1. The name may be from one to six characters.
  2. The first character must be a letter.
  3. Characters other than the first may be letters or numeric digits.
  4. If the first character is I, J, K, L, M or N, the variable is integer (i.e. can hold a whole number value). Otherwise, it is real (i.e. can hold a value according to the floating point convention).

The values of variables may be changed during execution of a program. Values which do not change are called constants, and are written simply as a number.

If a decimal point is included, the constant is real; otherwise it is integer. Constants may also be preceded by a sign, e.g. -2, +3.5, -0.02, +360.

Variables and constants can be combined by operators to make expressions. Examples of operators are

        +  addition
        -  subtraction
        *  multiplication
        /  division

The variables and constants in an expression should be of the same type, i.e. all real or all integer.

FORTRAN statements are typed in on lines of 80 chracters. If position 1 contains a C or an *, the line is a comment and is ignored by the compiler. Normal statements are in the following format:

  1. Positions 1-5. Either a numeric label in the range 1-99999, or left blank.
  2. Position 6. Left blank.
  3. Positions 7-72. The body of the statement.
  4. Positions 73-80. Ignored by the compiler. They may be used for a sequence number.

Statements too long for one line may be continued on subsequent lines which do not have a blank in position 6.

An assignment statement is used to place a value in a variable. It takes the form:

        v = exprn
where ``v'' is a variable and ``exprn'' is an expression. The expression is evaluated, and the result is placed in the variable. If the expression is not the same type as the variable, the results is converted to the type of the variable before being placed in it.

A READ statement is used to bring values into the computer and leave them in computer cells. It has the form:

        READ (u, f) v
Here ``u'' is the unit number which shows the input stream to be read. The most commonly used unit number for input from a keyboard is 5. The ``f'' is the label of a FORMAT statement which is to be consulted for information regarding the layout of the number on the line. The variable into which the number will be placed when it has been read is indicated as ``v''.

A WRITE statement is used to take values from computer cells and to send them to some output device. It takes the form:

        WRITE (u, f) v
where ``u'' is the unit number for the output (6 is the most common unit number for the lineprinter), ``f'' is the label of a FORMAT statement which gives information regarding the layout of the number on the printed page, and ``v'' represents a variable whose value is to be written out.

A FORMAT statement has the form:

      f FORMAT (list)
where ``f'' represents a numeric label in the first five positions, and ``list'' represents one or more format codes separated by commas. Some examples are:
  1. ``I10''
    When reading, this means that ten positions of the line are to be taken and converted into an integer number. When writing, it means that an integer number is to be printed in ten positions on the page.
  2. ``F10.2''
    When reading, this means that ten positions of the line are to be taken and converted into a floating point number. If no decimal point is found in the ten positions, then the last two colums should be considered to be beyond the decimal point. When writing, it means that a floating point number is to be written out using ten positions on the page. The decimal point is to be printed two positions before the end of the number.
  3. ``1H '' (note the blank after the H)
    When writing, this means that one character, namely the blank, is to be inserted into the line being built up for despatch to the lineprinter.

B.3. Jumps and Loops

A GO TO statement is used to send control to another statement unconditionally. It has the form:
        GO TO label
Control is sent to the statement which has the specified label.

An IF statement may be used for conditional transfer of control:

        IF (condition) GO TO label
If the condition is true, the trailer GO TO is excuted. Otherwise the next statement is executed.

The condition in the IF statement consists of two expressions (either both real or both integer) separated by a relational operator.

The DO statement is used to define a loop which is to be obeyed a certain number of times. It has the form:

        DO label index = n1, n2
The statements from the one immediately following the DO statement up to and including the one with the specified label are performed a number of times as the index takes on all successive increasing integer values from n1 to n2.

In the DO statements n1 and n2 may be integer variables or unsigned integer constants, but may not be zero or negative. The index must be an integer variable. The value of the index may be used within the loop, but must not be changed within the loop.

The final statement of the DO loop must be executable, and may not be a conditional or unconditional transfer of control.

The CONTINUE statement is an executable statement which does nothing. It is only useful when it has a label, as it may then be used to end a DO loop if the previous statement would be unsatisfactory as the end of the loop.

B.4. Reading, Writing and Arithmetic

A READ statement may have the form:
        READ (u, f) list
where the list consists of one or more variables separated by commas. Reading will continue until all the variables in the list have been filled with values.

A WRITE statement may also have a list. Writing continues until all the variables in the list have been written out.

The I format code has the form:

        Iw
where ``w'' represents the number of positions on the lines from which the integer is to be taken, or the number of print positions which it is to occupy on the printed page.

The F format code has the form:

        Fw.d
where ``w'' has the same meaning as for I format, and ``d'' represents on input the number of places assumed beyond the decimal point (unless a decimal point is typed on the line), and on output the number of places beyond the decimal point which are to be printed.

A FORMAT statement may have several format codes separated by commas. These define successive fields across the line or the printed line. A repeat count may be used:

        nIw     nFw.d
which means that ``n'' successive fields are defined.

Reading or writing causes the I/O list to be matched with the format codes. If the end of the FORMAT is reached before the end of the I/O list, rescan occurs. This means that the FORMAT is re-used from the beginning, and a new line is begun. If the end of the I/O list is reached before the end of the FORMAT, the I/O operation ceases.

Expressions may occur in assignment or IF statements, but not in an I/O list. There is an additional operator:

        **  exponentation (raising to a power)
The result of ``integer ** integer'' is integer. The result of ``real ** integer'' or ``real ** real'' is real.

Certain functions are provided in FORTRAN, e.g. the square root function SQRT. To find the square root of a value specified as a real expression, one writes:

        SQRT (exprn)
The order of evaluation of expressions may be controlled by means of brackets. Those parts of an expression which are within brackets are evaluated before those outside. The order of evaluation is (from top to bottom):
        fn (  )  functions
        (  )     sub-expressions within brackets
        **       exponentiation
        *  /     multipliction and division
        +  -     addition and subtraction
when an expression contains several operators at the same level, the evaluation proceeds from left to right.

Real constants may be written with an E-exponent, e.g. 0.248E3, 78.0E+17, -0.02E4, 6.8E-10.

Division of one integer by another produces an integer result which may not be the integer nearest to the fractional result.

B.5. A New Dimension

A block of cells (an array) with a single name can be reserved by means of the statement:
        DIMENSION name (length)}
This is a non-executable statement, instructing the compiler to reserve this block of cells. It must precede the first executable statement. As the number of cells needed must be known at compilation time, the length must be a constant. All the cells in the array are the same type, determined by the type of the name used (integer or real).

When an array has been reserved, an individual cell (an element) may be referred to as

        name (subscript)}
The subscript may take one of the seven standard forms: ``c'', ``v'', ``v+c'', ``v-c'', ``c*v'', ``c*v+c'', ``c*v-c'', where ``c'' and ``v'' represent integer constants and integer variables respectively.

Several arrays may be reserved by one DIMENSION statement:

        DIMENSION name (length), name (length)}
A two-dimensional array may be reserved by means of a statement of the form:
        DIMENSION name (length1, length2)}
When elements of this array are subsequently referred to, it must be as:
        name (subscript1, subscript2)}
where each of the subscripts are one of the seven standard forms. Up to three dimensions are permitted in FORTRAN. Such arrays are reserved by the compiler as linear arrangement of cells, with the first subscript varying most rapidly, the second subscript varying next most rapidly, and so on.

If control passes out of a DO loop by means of a transfer of control, the value of the index of the loop is preserved. If the loop is performed the full number of times and control passes on from the final statement of the loop, the value of the index is undefined, i.e. the value will depend on the particular compiler.

DO loops may be nested one inside another to an indefinite depth. The indices of nested loops must be different. Several loops may end on the same statement.

B.6. Formats

Character (or Hollerith) strings may be used anywhere within a FORMAT statement. They take the form ``nH'' followed by ``n'' characters or they are enclosed in quotes `` 'text string' ''.

The first character of each line sent to the line printer is used for carriage control purposes. There are four valid carriage control characters in standard FORTRAN:

        ' '  The paper is moved up one line before printing.
        'O'  The paper is moved up two lines before printing.
        'I'  The paper is moved to the top of a new page before printing.
        '+'  The paper is not moved.
The whole of an array may be read or written by specifying the name of the array in an I/O list. Two- and three-dimensional arrays are read or written in the order in which they are stored in the computer, i.e. with the first subscript varying most rapidly.

Several format codes may be surrounded by brackets, in which case they are called a group. Such a group may be preceded by a repeat count indicating the number of times it is to be repeated when the format is used. The maximum depth of nesting of brackets in a FORMAT statement must not exceed three.

The rule for rescanning formats is complicated by the presence of brackets surrounding groups of format codes. The full rule for rescan is as follows. If there are no nested brackets, rescan returns to the beginning of the format. Otherwise, the rescan returns to the left bracket which matches the right bracket nearest to the closing bracket of the format. If this left bracket is preceded by a repeat count, this count takes effect during the rescan.

The format code ``nX'' is used to space over ``n'' positions of the line on input, or to leave ``n'' print positions blank on output.

The format code ``Ew.d'' is identical to the format code ``Fw.d'' on input. Either code will accept floating point numbers with exponents. On output ``Ew.d'' will print out a number in exponent and fraction form, the total field width being ``w'' places, and ``d'' the number of significant digits in the fraction.

Format code ``An'' is used to read characters into either an integer or real variable, or to write out the contents of a variable as characters. On input, ``n'' characters are placed in the cell (if there is room), and any remaining space in the lower end of the cell is filled with clank characters. On output the left-most ``n'' characters are printed.

Format code ``/'' is used to begin a new record. On input, if indicates that a new line is to be read. On output, it indicates the start of of a new line being built up for the printer.

A READ or WRITE statement continues until (1) the I/O list is finished and (2) either the end of the FORMAT statement is reached, or the next format code requires the transmission of information to or from a variable.

B.7. Fine Control

The DO statement may specify the increment, i.e. the amount by which the index will be increased each line round the loop. The full form of the DO statement is:
        DO label index = n1, n2, inc
The ``index'' must be a non-subscripted integer variable, and ``n1'', ``n2'' and ``inc'' must be unsigned integer constants or integer variables, and may not be nagative or zero.

A READ or a WRITE statement may use an implied DO loop as part of its list:

        (list, index = n1, n2, inc)
If ``inc'' is omitted, an increment of 1 is assumed. Implied DO loops may be nested, i.e. the list inside an implied DO loop may include another implied DO loop.

The trailer of a logical IF statement may be any executable FORTRAN statement except a DO or another logical IF. Several conditions may be joined together by means of the operators .AND. and .OR. to produce a compound condition.

The arithmic IF statement has the form:

        IF (exprn) label1, label2, label3
The expression may be either integer or real. It is evaluated, and the spin of its result is tested. If it is negative, control passes to the statement having ``label1''. If it is zero, control passes to ``label2'' and if it is positive, to ``label3''.

The computed GO TO statement has the form:

        GO TO (label1, label2, ...), index}
The ``index'' must be an unsubscripted integer variable. The value of the ``index'' controls the transfer of control. If the ``index'' is 1, control is passed to ``label1'', if it is 2, to ``label2'', and so on.

B.8. Functions and Subroutines

A function subprogram may be written to calculate single values. This will begin with the statement:
        FUNCTION name (parameter list)
Inside the function the ``name'' is used as if it were a variable. Control is passed back to the calling program by means of the statement:
        RETURN
The function is called by including in an expression:
        name (argument list)
On return from the function the value left in the function name takes the place of the function reference in the evaluation of the expression.

A subroutine subprogram may be written to do a job of work. The first statement must be:

        SUBROUTINE name (parameter list)
or
        SUBROUTINE name
if the subroutine has no parameters. The name of the subroutine must not be used as if it were a variable. Values to be returned by the subroutine (if any) must be placed in the dummy parameters, which causes these values to be placed in the arguments handed over to the subroutine. Control is returned to the calling program by the statement:
        RETURN
The subroutine is called by the statement:
        CALL name (argument list)
or:
        CALL name
depending on whether the subroutine has parameters or not.

Certain rules apply to both functions and subroutines. The arguments must match the parameters in number, the type of each, and whether they are dimensioned. A DIMENSION statement inside a subprogram may use an integer variable (unsubscripted) for the length, provided that the array concerned and the integer variable are both parameters. When the array is a parameter, no space is reserved for it by the DIMENSION statement in the subprogram. Variable names or statement labels which have been used in the main program (or in another subprogram) may be used in a subprogram without fear of a clash. Functions and subroutines must, like the main program, finish with an END statement.

B.9. Advanced Features

According to the implicit convention of FORTRAN regarding types, a variable is real or integer according to the first letter of its name. It is possible to override this impliciet convention by means of type statements, which take the form:
        type list
where the type is INTEGER, REAL, DOUBLE PRECISION, COMPLEX, CHARACTER or LOGICAL, and the list is a sequence of variable names separated by commas. Variables may be dimensioned in a type statement by including the length(s) in brackets after the name.

A double precision constant is similar to the E form of a real constant, but has a D in place of the E.

A complex constant consists of two real constants enclosed in brackets.

There are only two logical constants, ``.TRUE.'' and ``.FALSE.''. A logical assignment statement has a logical expression on the right hand side. The expressions inside the brackets of a logical IF are logical expressions. ``.NOT.'' is a logical operator which reverses the value of the following variables or paranthesised logical expression.

The DATA statement is used to cause the compiler to leave values in certain variables when it has finished compiling. It takes the form:

        DATA list/constants/, list/constants/, ...
where there are as many constants between the slashes as variables in the preceding list. Constants may be repeated by a replication factor, ``n*constant'', which is the same as writing the constant out ``n'' times. Hollerith constants may also be used in a DATA statement, to cause the compiler to leave characters in certain variables.

Two or more variables can be forced to share the same location by means of the EQUIVALENCE statement. This takes the form:

        EQUIVALENCE (list), (list), ...
The variable names in the first list will all be references to the same computer cell, the variable names in the second list will be references to another computer cell, and so on. If elements from two arrays are equivalenced, this will force all the other elements in the two arrays to be in a certain relationship.

Areas of storage which lie outside all the program segments may be reserved by means of the COMMON statement:

        COMMON /name/ list /name/ list ...
Several program segments may refer to the same area by including COMMON statements for the same name. The variables in the list need not be given the same name in different segments. Variables may be dimensioned in the COMMON statement as in a type statement. A special type of common area is blank common, which is reserved by a COMMON statement with no name between the slashes, or with no slashes. All other common areas are called named common.

Variables in blank common must not be initialised by a DATA statement. Variables in named common may only be initialised by a DATA statement within a BLOCK DATA subprogram. This begins with the statement:

        BLOCK DATA
and finishes with an END statement. The only statements permitted within this subprogram are DIMENSION, COMMON, EQUIVALENCE type and DATA statements.

DIMENSION, COMMON, EQUIVALENCE and type statements may occur in any order among themselves (in standard FORTRAN), but must all precede the first executable statement. These statements apply only to the program segments in which they occur. DATA statements may occur anywhere within a program segment.



Updated 30-jan-1996, pfk