next up previous contents index
Next: PARAMETER Statement Up: ConstantsVariables, and Arrays Previous: Guidelines

Named Constants

    The PARAMETER statement can be used to give a symbolic name to any constant. This can be useful in several rather different circumstances.

With constants of nature (such as tex2html_wrap_inline3107 ) and physical conversion factors (like the number of pounds in a kilogram) it can save typing effort and reduce the risk of error if the actual number is only given once in the program and the name used everywhere else:

 
      REAL PI, TWOPI, HALFPI, RTOD 
      PARAMETER (PI = 3.14159265,  TWOPI = 2.0 * PI) 
      PARAMETER (HALFPI = PI / 2.0,  RTOD = 180.0 / PI)
The names PI, TWOPI, etc. can then be used in place of the literal constants elsewhere in the program unit. It is much better to use named constants than variables in such cases as they are given better protection against inadvertent corruption: constants are often protected by hardware. The use of symbolic names rather than numbers can also make the program a little more readable: it is probably harder to work out the significance of a number like 1.570796325 than to deduce the meaning of HALFPI.

Another important application of named constants is for items which are not permanent constants but parameters of a program, i.e. items fixed for the present but subject to alteration at some later date. Named constants are often used to specify array bounds, character-string lengths, and so on. For example:

 
      INTEGER MAXR, MAXC, NPTS 
      PARAMETER (MAXR = 100, MAXC = 500, NPTS = MAXR*MAXC) 
      REAL MATRIX(MAXR,MAXC), COLUMN(MAXR), ROW(MAXC)
The constants such as MAXR and MAXC can also be used in the executable part of the program, for instance to check that the array subscripts are in range:
 
      IF(NCOL .GT. MAXC .OR. NROW .GT. MAXR) THEN 
          STOP 'Matrix is too small'  
      ELSE 
          MATRIX(NROW,NCOL) = ROW(NCOL) 
      END IF
If, at some point, the matrix turns out to be too small for your needs then you only have to alter this one PARAMETER statement: everything else will change automatically when the program is recompiled.

The rules for character assignment apply to PARAMETER statements: see section 7.4. In addition a special length specification of *(*) is permitted which means that the length of item is set to that of the literal constant. The type specification must precede the PARAMETER statement.

 
      CHARACTER*(*) LETTER, DIGIT, ALPNUM 
      PARAMETER (LETTER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
     $  DIGIT = '0123456789', ALPNUM = LETTER // DIGIT) 
      CHARACTER WARN*(*) 
      PARAMETER (WARN = 'This matrix is nearly singular')
The constant ALPNUM will be 36 characters long and contain all the alpha-numeric characters (letters and digits).

Named logical constants also exist, but useful applications for them are somewhat harder to find:

  
      PARAMETER (NX = 100, NY = 200, NZ = 300, NTOT = NX*NY*NZ) 
      LOGICAL LARGE 
      PARAMETER (LARGE = (NTOT .GT. 1000000) .OR. (NZ .GT. 1000))


next up previous contents index
Next: PARAMETER Statement Up: ConstantsVariables, and Arrays Previous: Guidelines

Mario Storti
Wed Nov 4 19:32:56 ART 1998