FORTRAN 77 Programming
P. A. Robinson
![]()
To run this program copy the code to a file in the School's Computing system and follow the instructions in the comments.
c ***************************
c A Mug's Guide to Fortran 77
c ***************************
c
c P. A. Robinson Fortran 77
c
c Modifications:
c 28 May 1990 Written
c 13 June 1990 Extended
c 21 March 1991 Corrected
c 10 December 1992 Modified
c 27 October 1993 Corrected
c
c This guide is intended to get the beginner started on Fortran. It is
c written in the form of a heavily commented Fortran program. Except
c where otherwise noted, comments precede the lines to which they refer.
c Comment lines must begin with the letter c. If you run this program,
c you will find that it compiles but does not do anything useful.
c
c Warning: do not type beyond the 72nd character on any line ---
c Fortran will ignore anything further --- a historical quirk.
c
c PROGRAM HEADER
c --------------
c
c All programs must have the program header, consisting of the word
c program and then the name of the program. All instructions must
c be indented 6 characters from the left margin --- another historical
c quirk.
c
c
c
c
program example
c ***************
c
c PARAMETERS
c ----------
c
c A parameter statement is used to permanently set the value
c of a parameter. This makes it easy to change parameters (e.g.
c array sizes) without searching the whole code. You cannot
c change the value of a parameter while the code is running.
c
parameter(n=50)
parameter(alpha=1.0)
c
c VARIABLE TYPES
c --------------
c
c The main types of variables in Fortran are integer, real and complex.
c Variables can be either single objects or arrays.
c Unless you specify otherwise in a type statement (to follow),
c variable names beginning with i -- n are assumed to be integers
c and all others are assumed to be real. Usually it is good
c practise to use these assumed definitions, even if you choose to
c give all the types explicitly --- it avoids confusion.
c You must always type arrays.
c
integer i,j,k,npoints
real able,baker,charlie
complex zcomplex
c
c Only the last of the above three type statements is necessary.
c
c ARRAYS
c ------
c
c Arrays of one or more dimensions may be defined. Unless otherwise
c specified, all array dimensions run from 1 to the array size.
c The first of the following arrays has 100 elements numbered 1 to
c 100, the second has 201, numbered -100 to 100.
c
real array(100),values(-100:100),arr(100),barray(100)
c
c Multidimensional arrays can also be defined. Moreover,
c parameter statements can be used in their definitions. The following
c define 100 by 100, 50 by 50 by 50, and 201 by 101 complex arrays:
c
complex carray(100,100), xarray(n,n,n), yarray(-100:100,-n:n)
c
c COMMON BLOCKS
c -------------
c
c Fortran allows subprograms to be defined: subroutines and functions
c (see below). One way to allow variables (including arrays) to be
c accessed by more than one subprogram is to put them in a so-called
c common block. The following statement allows the variables a, b
c and carray to be accessed by any subprogram that contains this
c statement. (Note that the sizes and types of the variables
c must also be declared in the subprogram.)
c
common/name/a,b,carray
c
c ASSIGNMENT STATEMENTS
c ---------------------
c
c The next statements give the value 5 to the integer variable k (note
c that there is no decimal point), the value 1.0 to the real variable able,
c the value 4k to the integer variable npoints, the value k/2.0 to
c baker (note that the decimal point is essential here or integer division
c will be done --- with remainders discarded!), and the value able
c squared to charlie.
c
k=5
able=1.0
npoints=4*k
baker=k/2.0
charlie=able**2.
c
c The following gives the value 1+2i to zcomplex
zcomplex=(1.,2.)
c
c CONTINUATION LINES
c ------------------
c
c If a line is too long to fit in the 72 character limit,
c it may be continued by putting virtually any character
c in the 6th column to indicate that there is a continuation:
c
x=
% 2*x
c
c IF STATEMENTS
c -------------
c
c The following gives the value 4.5 to able if i equals 1:
c
if(i.eq.1)able=4.5
c
c The following sets able to 4.5 and baker to tan(able) if i is zero:
c
if(i.eq.0)then
able=4.5
baker=tan(able)
end if
c
c The following sets able = 1.0 if able is positive; able=0.0 if able
c is zero; able=-1.0 in all other cases. You can have multiple
c statements within any subsection ("branch") of the IF statement.
c
if(able.gt.0)then
able=1.0
else if(able.eq.0)then
able=0.0
else
able=-1.0
end if
c
c .AND. AND .OR.
c --------------
c
c You can have multiple conditions deciding the fate of an IF block
c by using the .and. and .or. statements --- note the periods before
c and after these keywords.
c
if((x.gt.2).and.(y.lt.4))then
x=y
else if((x.ne.0).or.(y.ge.0))then
x=-y
end if
c
c .EQ. .NE. .GT. .LT. .GE. .LE.
c -----------------------------
c
c These keywords stand for equal to, not equal to, greater than, less
c than, greater than or equal to, and less than or equal to,
c respectively. The periods before and after are essential.
c
c DO LOOPS
c --------
c
c DO loops are used to repeat the same set of instructions many
c times over, for example, when accessing all the elements of an
c array one at a time.
c
c This statement multiplies all the elements in the array "arr" by 5.0
c Note that the label 10 (it must be a different number for each
c loop) appears after the word "do" and in the margin next to the
c closing statement "continue". The dummy variable i is used here
c to step over the array elements.
c
do 10 i=1,100
arr(i)=5.0*arr(i)
10 continue
c
c This loop adds the cubes of every second element in arr
c to tot, which is set to zero initially.
c
tot=0.0
do 20 i=1,100,2
tot=tot+arr(i)**3.0
20 continue
c
c This loop sums all the (complex) cosines of the elements in
c yarray. Note that there are two nested loops, one for each
c dimension, each with its own dummy variable as a counter (i and j
c here). Note that the parameter n is used in the bounds of one
c loop.)
c
zcomplex=(0,0)
do 30 i=-100,100
do 40 j=-n,n
zcomplex=zcomplex+cos(yarray(i,j))
40 continue
30 continue
c
c GOTO STATEMENT
c --------------
c
c The goto statement causes the execution of the program to jump to
c the specified point, given by a label in the margin. For example,
c the following statement causes execution to jump to the label 50
c (which happens to be on the following line here, so not much is
c accomplished in this case).
c
goto 50
50 continue
c
c Avoid goto statements at all cost!!! Otherwise your code will
c soon develop the structure of spaghetti. NEVER use a goto
c statement to jump out of a do loop or if-then-else block ---
c spaghetti will soon follow. About the only thing you need a goto
c statement for is to jump back to the start of a program at the end
c so that it can be run again without terminating execution and
c restarting.
c
c INPUT/OUTPUT
c ------------
c
c This prints a message to the terminal (unit 6):
c
print*,'a message'
c
c This writes an integer in a space 8 characters wide to the terminal
c (unit 6). Note that the format statement must have a label (1000
c here) that appears in the margin:
c
write(6,1000)i
1000 format(i8)
c
c This writes the real variables able and baker to a file fort.7 (unit 7)
c in the format of 2 floating-point numbers with 6 significant figures in
c a space 13 characters wide (some of which are used for the exponent etc):
c
write(7,1005)able,baker
1005 format(2e13.6)
c
c This prints a message requesting you to enter the variable baker,
c then reads a real number from the keyboard (unit 5) allowing for a 12
c digit real number with 6 significant figures. Numbers such as 1.0456e-12
c are allowed, where the e-12 signifies an exponent of -12. Note that
c you don't have to type in all the significant digits (missing ones
c will be treated as zeros, but the decimal point is essential):
c
print*,'baker='
read(5,1010)baker
1010 format(f12.6)
c
c This reads zcomplex from unit 7, assuming unit 7 was written in the
c format 2e13.6:
c
read(7,1015)zcomplex
1015 format(2e13.6)
c
c This prints a message to unit 7.
c
write(7,1020)
1020 format('a message')
c
c This writes ' the value of i is' followed by the value of i, to the
c screen:
c
write(6,1025)i
1025 format(' the value of i is',i8)
c
c There is an easier way of doing i/o when you don't care exactly what
c format is used. Use of an asterisk causes the machine to decide on
c the appropriate format. Examples are as follows:
c
read(*,*)able,baker,i
c
c This reads able, baker and i from the keyboard.
c
write(*,*)able,baker,i
c
c This writes able, baker, and i to the terminal.
c
write(7,*)able,baker,i
c
c This writes able, baker, and i to unit 7.
c
c
c INTRINSIC FUNCTIONS
c -------------------
c
c A large number of standard functions are part of Fortran, including
c as sin, cos etc. See the manual for their usage. Some obsolete
c versions csin, ccos etc, tailored specifically to complex variables still
c exist. Don't use these obsolete versions --- the standard names take care
c of everything, regardless of variable type.
c
c LIBRARY FUNCTIONS
c -----------------
c
c Fortran provides a standard library of functions for random number
c generation etc. See the manual.
c
c SUBROUTINES
c -----------
c
c This calls the user-defined (see later for definition) subroutine
c addup that adds all the values in the array barray and returns
c the result total
c
call addup(barray,total)
c
c FUNCTIONS
c ---------
c
c This calls the user-defined real function (see later for definition)
c croot. Note that the type of croot needs to be defined at the top of this
c program where the typing is not implicit.
c
z=croot(x,y)
c
c It is good practise to include a stop statement before the final end
c statement. A stop statement can also be used to stop execution at
c other points in the code.
c
stop
end
c
c
c EXAMPLE SUBROUTINE
c ------------------
c
c The following is the subroutine called from the main program.
c The subroutine header contains the word "subroutine", followed
c by the name of the subroutine and a list of arguments in parentheses.
c Note that arguments can serve as inputs, outputs, or both.
c
c
subroutine addup(barray,total)
c ******************************
c
real barray(100)
c
do 10 i=1,100
total=total+barray(i)
10 continue
c
c The final two lines of a subroutine must be "return" and "end",
c respectively.
c
return
end
c
c
c EXAMPLE REAL FUNCTION
c ---------------------
c
c The following user-defined real function returns the cube root of the
c product of its two arguments if this product is positive, and zero
c otherwise. Every function must have a type and the function name must
c be assigned a value within the function itself.
c
real function croot(x,y)
c ************************
c
if((x*y).gt.0)then
croot=(x*y)**(1/3.0)
else
croot=0
end if
c
c The last two lines of a function must be "return" and "end", respectively.
c
return
end