Fortran 77 Summary User Guide

This is intended to be a brief summary description of and example illustration of the features of the Fortran 77 language.

The following sections are provided:

source layout
showing how source for programs, subroutines, functions and block data modules are built in Fortran 77
variable declaration
including: implicit typing rules; parameter statements for setting constant values in a Fortran program; and array declarations
variable initialization
including: data statements; data sharing using common blocks; and storage association using the equivalence statement
iteration
the do-loop and the use of continue statements
branching
the goto statements and statement labels
conditionals
the if statements including if-else-endif statements
Input/Output
including read/write statements, files and channels and format specification statements
intrinsic functions
supplied with Fortran 77
deprecated/redundant features
such as: the arithmetic IF statement; the computed goto statement; the assigned goto statement; and namelists
other non-standard features
often available in Fortran compilers such as the include statement; the enddo statement; and miscellaneous extra intrinsic functions and subroutines.

Source Layout

Fortran 77 source code layout dates back to the time when source was stored on punched cards. These cards had 72 columns and source layout required: Fortunately most compilers now allow lower case and variables are usually allowed to have at least 11 characters - often more.

For example: (F77 example 001)

C npac F77 example 001, showing source code layout.
C
C2345678--------This is a comment---------------------------------------v
      program myprog
      integer i, ifunc
      common /mycom/ i
      write(6,100) 'Hello Ken'
  100 format(1x,A)
      i = i + 1000
      call mysub
      i = ifunc( i )
      write(6,*) i
      stop
      end

      subroutine mysub
      integer i
      common /mycom/ i
      i = i + 1
      return
      end

      integer function ifunc( k )
      integer k
      ifunc = k * k
      return
      end
   
      block data myblck
      common /mycom/ i
      data i /12345/
      end
C----------------------------------------------------------------------^
The example code above shows how: program; subroutine; function; and block data modules are laid out in a source file.

Each program must have one and only one program module. Any number of functions and subroutines can be in the same file or in other files to be linked later.

A program is the module that is run first when the compiled and linked application is run. A subroutine is a procedure or module that can be called from a program, or from another subroutine or from a function. A function in Fortran is like a subroutine except that it returns a value. The block data module is the only permisssable way to initialise common data.


Variable Declaration

In Fortran the data types available are INTEGER for whole numbers; REAL for floating point numbers; DOUBLE PRECISION for floating point numbers with twice the storage requirements of REALs; COMPLEX for complex numbers stored as real/imaginary number pairs; LOGICAL for Boolean values; and CHARACTER for textual characters.

By default, Fortran 77 has an implicit typing scheme whereby a variable not explicitly declared will be automatically declared with a type determined by the first letter of its name. Specifically, by default, variables starting with the letters A,B,C,...H and O,P,Q,...Z will be REAL, variables starting with the letters I,J,K,L,M,N will be integers.

The default behaviour can be changed using the implicit statement as shown in the example.

Parameter values can be set in Fortran 77 as fixed at compile time by using the PARAMETER statement as shown in the example.

Array declaration in Fortran 77 is done either with a list of comma separated integers giving the size in each dimension of the array to be declared. The Fortran 77 standard provides for up to seven dimensional arrays, although some implementations provide more. Array indexing is by default from 1 up to the size in each dimension, although arrays can be declared as ARRAY(0:N) or even as ARRAY(-N:N), which would have N+1 and 2N+1 elements respectively.

For example: (F77 example 002)

      program myprog
      integer maxm, maxn
      parameter( maxm = 20 )
      parameter( maxn = 10 )
      integer m, n, i, j

      real a(m,n), b(n), c(1:n)
      m = 5
      n = 8
      call mysub( a, b, c, m, n, maxm, maxn )

      end

      subroutine mysub( a, b, c, m, n, maxm, maxn )
      integer m, n, maxm, maxn
      real a(maxm,maxn), b(maxn), c(maxn) 
      do 20 j=1,n
        do 10 i=1,m
          a(i,j) = 0.0
   10   continue
        b(j) = 0.0
        c(j) = 0.0
   20 continue
      return
      end
C----------------------------------------------------------------------^
The example shows a (fairly useless) program which sets up two vectors and an array, and then calls a subroutine to initialize them all to zero. The sizes can be passed from the program to the subroutine at run time, which is usefil if the subroutine is part of a library and should not have sizes hard-wired into it.


Variable Initialization

In Fortran 77 data can not be assumed to be initialized to anything unless done so explicitly by the programmer. Local variables can be initialized using the data stament as shown in the example. Shared or COMMON data must be initialized using a block data module - to guarantee it is initialized in only one part of the program as it would be ambiguous to initialize it twice. data statement common block equivalence statement

Iteration

do-loop continue statement

Branching

goto statement statement label

Conditionals

if statement if-else-endif statement

Input/Output

read/write channels

The Standard format specification elements in common use are:

A         Character(s) - Until run out of input or storage.
An        Character string exactly n characters long.
Dw.d      Floating D exponent format with w characters,
            and d decimal places.
Ew.d      Floating E exponent format with w characters,
            and d decimal places.
Fw.d      Floating format with w characters, d decimal places.
Gw.d      Floating format (tries F if possible, else E or D).
nHabcd    Explicit number n (4 here) of characters abcd.
Iw        Integer format with w characters.
Lw        Logical format with w characters.
Tn        Moves to column n with n on [1,80]
nX        Ignore n spaces.
'text'    Explicit quotation of text.
/         New line (I'm not sure if this is standard).

Intrinsic Functions

The following Intrinsic functions are provided by FORTRAN-77:
ABS(a),  IABS(a)     Absolute value returning REAL, or INTEGER.
AINT(a),  INT(a)     Integer value returning REAL or INTEGER.
ANINT(a), NINT(a)    Nearest Integer value returning REAL or INTEGER.
REAL(a)              Conversion to REAL.
AIMAG(a)             Imaginary part of a COMPLEX.
DBLE(a)              Conversion to DOUBLE PRECISION.
MOD(a1,a2)           Remainder from a1/a2.
MAX(ai,...)          Maximum value of two or more arguments.
MIN(ai,...)          Minimum value of two or more arguments.
SIGN(a1,a2)          Transfer of sign |a1|Sign(a2) returning REAL
ISIGN(a1,a2)         Transfer of sign |a1|Sign(a2) returning INTEGER.
DIM(a1,a2)           Positive difference | a1 - a2 |.
EXP(a)               Exponential e^a1.
LOG(a)               Natural Logarithm.
LOG10(a)             Base-10 Logarithm.
SIN(a)               Radian Sine.
COS(a)               Radian Cosine.
TAN(a)               Radian Tangent.
ASIN(a)              Radian Arcsine.
ACOS(a)              Radian Arccosine.
ATAN(a)              Radian Arctangent.
ATAN2(a1,a2)         Radian Arctangent of (a1/a2)
SINH(a)              Hyperbolic Sine
COSH(a)              Hyperbolic Cosine
TANH(a)              Hyperbolic Tangent

CHAR(i)              Character represented by i.
ICHAR(s)             INTEGER representation of character s.
LEN(s)               INTEGER length of string s.
INDEX(s1,s2)         INTEGER position of substring s2 in s1.

Deprecated/Redundant Features

arithmetic IF statement computed goto statement assigned goto statement namelists

Non-Standard Features

include statement enddo statement

The following non-standard miscellaneous extra intrinsic functions are provided on many Fortran compiler implementations (specifically on DEC systems)


IAND(i1,i2)          Bitwise AND.
IOR(i1,i2)           Bitwise OR.
IEOR(i1,i2)          Bitwise Exclusive-Or.
NOT(i1)              Bitwise inversion.
ISHFT(i1, n1)        Left shift of bits in i1 by n1, losing bits off
                     the end.  ( = x2)
IBITS(i1, n1, n2)    Extract bits n1 to n1+n2-1 of i1.
IBSET(i1, n1)        Set bit n1 of i1.
IBCLR(i1, n1)        Clear bit n1 of i1.
BTEST(i1, n1)        .TRUE. if bit n1 of i1.
ISHFTC(i1, n1)       Circular right shift of i1 by n1 (= /2)

DATE(STRING)         Nine character date in form: dd-mmm-yy.
IDATE(I,J,K)         The date: I=month (1..12)
                               J=Day (1..31)
                               K=Year (89..99)
SECNDS(X)            Returns INTEGER number of seconds since midnight
                     minus X seconds.
TIME(STRING)         Eight character time in form: hh:mm:ss.

Ken Hawick, hawick@npac.syr.edu