Next: , Previous: Label Declaration, Up: Source Structures

6.1.3 Constant Declaration

A constant declaration has the following look:

       constant_identifier = constant_expression;
       constant_identifier = constant_expression;

A constant declaration part starts with the reserved word const. It declares a constant_identifier which is defined by constant_expression. This expression has to be evaluatable during compilation time, i.e. it can include numbers, parentheses, predefined operators, sets and type casts (the last, however, is a Borland extension). In ISO 7185 Pascal, constant_expression must be a constant or a set. All Pascal Dialects but ISO-Pascal allow the use of these intrinsic functions in constant_expression:

Abs, Round, Trunc, Chr, Ord, Length, Pred, Succ, SizeOf, Odd.

In Borland Pascal, in the constant declaration part variables can be declared as well, which are given an initial value. These variables are called “typed constants”. It is good style to avoid this use, especially since Extended Pascal and GNU Pascal allow to initialize a variable in variable declaration part or give a type a preset value on declaration.

       FiveFoo      = 5;
       StringFoo    = 'string constant';
       AlphabetSize = Ord ('Z') - Ord ('A') + 1;
       PInteger     = ^Integer;     { Define a pointer to an Integer }
       { Constant which holds a pointer to an Integer at address 1234 }
       AddressFoo   = PInteger (1234);

See also

Subroutine Parameter List Declaration