Different manufacturers like to add extensions to 'their' version of FORTRAN 77 and a number of these extensions are widely supported. For truly portable software you should avoid their use but if portability is not an issue, then the nonstandard statements can be very useful to the programmer.
In FORTRAN 77, all constants, variables and arrays are assigned an
implicit type according to the first letter of the symbolic name unless it
is overridden by an explicit type declaration. This can lead to subtle errors
as misspelled symbolic names are not picked up by the compiler but merely
assigned the appropriate type. The IMPLICIT NONE
statement at the beginning of the type declarations in a program unit turns
off implicit typing and forces the programmer to explicitly declare the type
of every constant, variable and array used.
The declarations
IMPLICIT NONE INTEGER I,NMAX PARAMETER(NMAX = 5000) CHARACTER*3 BAYER(NMAX),CONST(NMAX) REAL DEC(NMAX),RA(NMAX)
at the beginning of a program unit define the variables named
I
and NMAX
to be INTEGER
;
BAYER
and CONST
to be length 3 CHARACTER
arrays of size NMAX
(which is given a value of 5000 in a previous
PARAMETER
statement); and DEC
and RA
to be REAL
arrays of size NMAX
. The
IMPLICIT NONE
statement means that if any other constant,
variable or array names appear in the program unit, the compiler will return
an error. This is a useful guard against mistyped symbolic names.
The INCLUDE
statement inserts the contents of a separate text
file into the source code in place of the INCLUDE
statement.
It is often used with specification statements where the same set of
specifications, including type declarations, PARAMETER
statements
and COMMON
blocks, is used in multiple program units. The
general form of the INCLUDE
statement is
INCLUDE 'file-name'
Some years ago, one of the authors wrote a FORTRAN 77 program to model
the structure and evolution of the Sun. Vast amounts of data had to be
passed between program units and the same physical constants had to be used
over and over again. Retyping this information in each program unit was
highly susceptible to error so many of the type declarations, constant
initialisations and COMMON
block declarations were put in
separate text files and INCLUDE
statements were used where
appropriate in the various program units.
As an example, a file called physix.h
contained assorted physical
constants used throughout the program. The file contained type and
COMMON
block declarations and looked like this:
C This common block contains physical constants in cgs units. Our C variables are as follows: C A radiation constant C A1-A14 atomic weights C C speed of light C CP specific heat at constant pressure C FOURPI 4 pi C G gravitational constant C GAMMA adiabatic constant C LSUN luminosity of the Sun C MSUN mass of the Sun C NA Avogadro's number C PI pi C RATIO (gamma - 1)/gamma C RATIO1 gamma/(gamma - 1) C RG gas constant C RSUN radius of the Sun C SECYR seconds per year DOUBLE PRECISION A,A1,A3,A4,A12,A14,C,CP,FOURPI,G,GAMMA,LSUN DOUBLE PRECISION MSUN,NA,PI,RATIO,RATIO1,RG,RSUN,SECYR COMMON /PHYSIX/ A,A1,A3,A4,A12,A14,C,CP,FOURPI,G,GAMMA,LSUN, $ MSUN,NA,PI,RATIO,RATIO1,RG,RSUN,SECYR
Note that the included file must be in proper FORTRAN 77 format. The main program then contained the following statements:
PROGRAM MAIN … IMPLICIT NONE INCLUDE 'coef.h' INCLUDE 'files.h' INCLUDE 'help.h' INCLUDE 'old.h' INCLUDE 'opacity.h' INCLUDE 'param.h' INCLUDE 'physix.h' INCLUDE 'time0.h' …
Note the use of the IMPLICIT NONE
statement to disable
automatic typing. INCLUDE
statements were used throughout the
program, including external functions and subroutines.
Why use INCLUDE
? It reduces error when the same block of code
is reused in many program units. Plus, if a change is made to that block of
code, it is only necessary to make the change in one place, rather than
having to hunt throughout the entire program, making the change in multiple
places.
One widely supported nonstandard FORTRAN 77 statement is
END DO
which is used in place of a labelled
CONTINUE
statement at the end of a DO
loop.
The general form of such a DO
loop is as follows:
DO loop-control-variable = initial-value, final-value, step-size statement1 statement2 … statementn END DO
Note that there is no label in the DO
statement, just the
loop-control-variable and its values.
SUM = 0 DO I = 1,100 SUM = SUM + I END DO
is equivalent to
SUM = 0 DO 10, I = 1,100 SUM = SUM + I 10 CONTINUE
A DO
loop works well if there is a countable number of
iterations that must be performed but sometimes it is necessary to loop
based on some logical criterion. FORTRAN 77 has no other built-in loop
structures although it is possible to write do-while and repeat-until loops
using IF
and GO TO
statements. However, one
common FORTRAN 77 extension is a formal DO WHILTE
statement. The general form of this statement is
DO WHILE (logical-expression) statement1 statement2 … statementn END DO
DO WHILE (A .GT. B) WRITE(*,*)'A = ',A,' and B = ',B A = A - 1 B = B + 1 END DO
is equivalent to
10 IF (A .GT. B) THEN WRITE(*,*)'A = ',A,' and B = ',B A = A - 1 B = B + 1 GO TO 10 END IF
Note the use of the END DO
statement to demarcate the end
of the loop, just like the alternative form of the DO
loop
discussed above.