SuivantPrec.Bas prec.BasNiv. sup.

5.4 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 p) 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))

PARAMETER Statement 

The general form of the PARAMETER statement is:       PARAMETER ( cname = cexp, cname = cexp, ... ) where each cname is a symbolic name which becomes the name of a constant, and each cexp is a constant expression of a suitable data type.

The terms in a constant expression can only be literal constants or named constants defined earlier in the same program unit. Variables, array elements, and function references are not permitted at all. Otherwise the usual rules for expressions apply: parentheses can be used around sub-expressions, and the arithmetic types can be intermixed. There is one restriction on exponentiation: it can only be used to raise a number to an integer power. The normal rules for assignment statements apply: for arithmetic types suitable conversions will be applied if necessary; character strings will be truncated or padded to the required length. Note that substring references are not permitted in character constant expressions.

PARAMETER statements are specification statements and may precede or follow type statements. But any type (or IMPLICIT) statement which affects the data type or length of a named constant must precede it. Subject to these rules, PARAMETER statements are permitted to precede IMPLICIT statements. This makes it possible for a named constant to set the default length for the character type for certain ranges of initial letters. For example:
       PROGRAM CLEVER
       PARAMETER (LENCD = 40, LENE = 2 * LENCD)
       IMPLICIT CHARACTER*(LENCD)(C-D), CHARACTER*(LENE)(E)
       PARAMETER (DEMO = 'This is exactly 40 chars long')
Once defined, a named constant can be used in any expression, including a dimension-bound expression, or in a DATA statement. A named constant cannot be used just as part of another constant (for example one component of a complex constant) and named constants are not permitted at all within format specifications.

Guidelines 

One of the limitations of Standard Fortran at present is that there is no way of allocating memory dynamically. One of the best ways around this is to use named constants to specify array bounds; this makes it much easier to alter programs to suit new requirements.

Names should also be given to all mathematical and physical constants that your programs require. If the same constants are needed in several program units then it may be sensible to compose a suitable set of PARAMETER statements for all of them and bring them in where ever necessary using INCLUDE statements.

If you define double precision constants in a PARAMETER statement do not forget that each literal constant value must include an exponent using the letter D.

There are no constant arrays in Fortran: the only way to overcome this limitation is to declare an ordinary array in a type statement and initialise its elements with a DATA statement (described in section 11).

SuivantPrec.Bas prec.HautNiv. sup.