Constant declarations

To declare a constant, add the keyword CONSTANT to a standard variable declaration:

CONSTANT { access } datatype constname = value

Only one constant can be declared per keyword CONSTANT:

CONSTANT int ii_first = 1
CONSTANT int ii_second = 2
CONSTANT int ii_third = 3

If you declare more than one variable after one keyword CONSTANT, only the first variable is declared as constant (the others are declared as variables).

In the following example, ii_first is a constant, ii_second and ii_third are declared as variables instead of constants.

CONSTANT int ii_first = 1, ii_second = 2, ii_third = 3

Only a datatype that accepts an assignment in its declaration can be a constant. For this reason, blobs cannot be constants.

Even though identifiers in PowerScript are not case sensitive, the declarations shown here use uppercase as a convention for constant names:

CONSTANT integer GI_CENTURY_YEARS = 100
CONSTANT string IS_ASCENDING = "a"

Advantages of constants

If you try to assign a value to the constant anywhere other than in the declaration, you get a compiler error. A constant is a way of assuring that the declaration is used the way you intend.

Constants are also efficient. Because the value is established during compilation, the compiled code uses the value itself, rather than referring to a variable that holds the value.