2-1  A FORTRAN-SPECIFIC PROBLEM - IMPLICIT DECLARATIONS 
 *******************************************************
 (Thanks to Keith Bierman and Craig Burley for the excellent comments)

 The general programming principles discussed in the programming section
 imply the simple rule: 

   +---------------------------------------------------+
   |  DECLARE EVERY VARIABLE AND USE MEANINGFUL NAMES  |
   +---------------------------------------------------+

 Don't be tempted to use FORTRAN automatic variable declaration
 capabilities! This is not a feature but a trap! 

 Declaring every variable without disabling implicit declarations 
 is not enough. Following this simple rule can save you a lot of 
 unnecessary nasty bugs.

 For example: 

    A mistyped variable name will be considered by the compiler as 
    a new variable (it'll possibly assign some default initial value, 
    e.g. 0) and computation will continue without any warning! 

    A missing comma between two variable names in a variable 
    declaration list (even if they are on different continuation 
    lines) will be considered by the compiler as a declaration 
    of one variable!

    A rather artificial example:


      PROGRAM MSTTYP
C     ------------------------------------------------------------------
      INTEGER
     *          chickn,
     *          start
     *          end
C     ------------------------------------------------------------------
      chicken = 10
      WRITE (*,*) chiken, startend
C     ------------------------------------------------------------------
      END



 Another view on implicit variable declarations
 ----------------------------------------------
 Well, as much as I have preached this [that all variables 
 should be explicitly declared], it really is too harsh.

 In an Algol based language, one can at least declare variables 
 locally say per loop. In Fortran, with a large subroutine, 
 is it really helpful to declare several (perhaps a hundred) 
 integer temps?

 A practical compromise is to use implicit logical for A-H, O-Z.
 Some processors have an UNDEFINED

	IMPLICIT UNDEFINED (A-H,O-Z)

 but this didn't catch on.



 Practical solutions
 ------------------- 
 The compiler can help you check that all variables are
 properly declared. 

 You can either put an 'IMPLICIT NONE' (non-standard) in the 
 beginning of every procedure and function or compile with a 
 compiler option:

   $ FORTRAN/WARNINGS=DECLARATIONS             (VMS)
     f77 -u                                    (SUN)
     f77 -u                                    (IRIX)
     f77 -u                                    (DUNIX)
     f77 -u                                    (ULTRIX)
     f77 /dclvar                               (Salford)
     f77 /0                                    (Lahey)


 A standard conforming alternative that is nearly as good is to put 
 'IMPLICIT LOGICAL (A-Z)' at the beginning of every procedure and 
 function. If the compiler encounters an untyped variable it will 
 assume it to be a logical variable. 

 Getting a logical result ('T', 'F') for a numeric calculation is 
 something that can't be overlooked, also logical variables are more 
 restricted than other types, so the compiler may flag a syntax error.

 A small example program (due to Arne Vajhoej) shows what happens to 
 logical variables in a numeric program:


      PROGRAM TEST
C     ------------------------------------------------------------------
      LOGICAL*4 
     *          A, B, C
C     ------------------------------------------------------------------
      A = 1
      B = 2
      C = A + B
      WRITE(*,*) C
C     ------------------------------------------------------------------
      END


 Some nice non-declaration (and other) traps by Arne Vajhoej:


      PROGRAM E1
C     ------------------------------------------------------------------
      INTEGER*4 
     *              I, J
C     ------------------------------------------------------------------
      I = 7
      J = I MOD 4
      WRITE(*,*) J
C     ------------------------------------------------------------------
      END


      PROGRAM E2
C     ------------------------------------------------------------------
      INTEGER*4 
     *              I, J
C     ------------------------------------------------------------------
      I = 4
      IF (I .EQ. 4) THEN J = 1
      WRITE(*,*) J
C     ------------------------------------------------------------------
      END


      PROGRAM E3
C     ------------------------------------------------------------------
      INTEGER*4 
     *              I, J
C     ------------------------------------------------------------------
      I = 3
      J = I
C    This
     is
C    a
C    comment
      WRITE(*,*) J
C     ------------------------------------------------------------------
      END


 If you can't find what is wrong with these examples, try to compile 
 and run them! If you do so, then retry, using IMPLICIT NONE right 
 after the PROGRAM statement, or use the appropriate compiler option 
 (see above -- e.g. '-u')".

  +-----------------------------------+
  |  DON'T USE IMPLICIT DECLARATIONS  |
  +-----------------------------------+

Return to contents page