[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A constant declaration has the following look:
const 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:
section 9.1 Abs, section 9.236 Round, section 9.282 Trunc, section 9.41 Chr, section 9.181 Ord, section 9.133 Length, section 9.201 Pred, section 9.271 Succ, section 9.257 SizeOf, section 9.176 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.
const FiveFoo = 5; StringFoo = 'string constant'; AlphabetSize = Ord ('Z') - Ord ('A') + 1; |
An example of a typed constant:
const i: Integer = 0; |
If you want to use it as a constant only, that's perfectly fine. However, if you modify `i', we suggest to translate the declaration to an initialized variable. The EP syntax is:
var i: Integer value 0; |
GPC supports this as well as the following mixtureof dialects:
var i: Integer = 0; |
Furthermore, you can also assign initialization values to types:
program InitTypeDemo; |
Here, all variables of type MyInteger are automatically initialized to 0 when created.
program BPArrayInitDemo; |
And the following way in EP:
program EPArrayInitDemo; |
There seem to be pros and cons to each style. GPC supports both as well as just about any thinkable mixture of them.
Some folks don't like having to specify an index since it requires renumbering if you want to add a new item to the middle. However, if you index by an enumerated type, you might be able to avoid major renumbering by hand.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |