Back to Contents


Constants

Summary:

See also: Variables, Records, Data Types, User Types.


Definition

A constant defines a read-only value identified by a name. A constant is similar to a variable, except that its value cannot be modified by program code.

Constants as typically used to define common invariable values that will be used at several place in a program:

01 CONSTANT PI DECIMAL(12,10) = 3.1415926,
02        MAX_SIZE INT = 10000,
03        ERRMSG = "PROGRAM ERROR: %1" -- type defaults to STRING

A good practice is to define constants that belong to the same domain in a single .4gl module, and import that module in the modules where the constants are needed.


Syntax

Syntax:

[PRIVATE|PUBLIC] CONSTANT constant-definition [,...]

where constant-definition is:

identifier [ datatype ] = value

Notes:


Usage

You can declare a constant to define a static value that can be used in other instructions. Constants can be global, module-specific, or local to a function.

By default, module-specific constants are private; They cannot be used by an other module of the program. To make a module constant public, add the PUBLIC keyword before CONSTANT. When a module constant is declared as public, it can be referenced by another module by using the IMPORT instruction.

When declaring a constant, the data type specification can be omitted. The literal value automatically defines the data type:

01 CONSTANT c1 = "Drink" -- Declares a STRING constant
02 CONSTANT c2 = 4711    -- Declares an INTEGER constant

However, in some cases, you may need to specify the data type:

01 CONSTANT c1 SMALLINT = 12000 -- Would be an INTEGER by default
Constants can be used in variable, records, and array definitions:
01 CONSTANT n = 10
02 DEFINE a ARRAY[n] OF INTEGER

Constants can be used at any place in the language where you normally use literals:

01 CONSTANT n = 10
02 FOR i=1 TO n
Constants can be passed as function parameters, and returned from functions.

Defining public constants in a module to be imported by others:

01 PUBLIC CONSTANT pi = 3.14159265
For date time constants, the value must be specified as an MDY(), DATETIME or INTERVAL literal:
01 CONSTANT my_date DATE = MDY(12,24,2011)
02 CONSTANT my_datetime DATETIME YEAR TO SECOND = DATETIME(2011-12-24 11:22:33) YEAR TO SECOND
03 CONSTANT my_interval INTERVAL HOUR(5) TO FRACTION(3) = INTERVAL(-54351:50:24.234) HOUR(5) TO FRACTION(3)

A constant cannot be used in the ORDER BY clause of a static SELECT statement, because the compiler considers identifiers after ORDER BY as part of the SQL statement (i.e. column names), not as constants:

01 CONSTANT pos = 3
02 SELECT * FROM customers ORDER BY pos

Automatic data type conversion can take place in some cases:

01 CONSTANT c1 CHAR(10) = "123"
02 CONSTANT c2 CHAR(10) = "abc"
03 DEFINE i INTEGER
04 FOR i = 1 TO c1 -- Constant "123" is converted to 123 integer
05 FOR i = 1 TO c2 -- Constant "abc" is converted to zero!

Character constants defined with a string literal that is longer than the length of the datatype are truncated:

01 CONSTANT s CHAR(3) = "abcdef"
02 DISPLAY s  -- Displays "abc"

The compiler throws an error when an undefined symbol is used in a constant declaration:

01 CONSTANT s CHAR(c) = "abc"  -- Compiler error: c is not defined.

The compiler throws an error when a variable is used in a constant declaration:

01 DEFINE c INTEGER
02 CONSTANT s CHAR(c) = "abc"  -- Compiler error: c is a variable, not a constant.

The compiler throws an error when you try to assign a value to a constant:

01 CONSTANT c INTEGER = 123
02 LET c = 345  -- Runtime error, c is a constant.

The compiler throws an error when the symbol used is not defined as an integer constant:

01 CONSTANT c CHAR(10) = "123"
02 DEFINE s CHAR(c)  -- Compiler error, c is a not an integer constant.

You typically define common special characters with constants:

01 CONSTANT c_esc  = '\x1b'
02 CONSTANT c_tab  = '\t'
03 CONSTANT c_cr   = '\r'
04 CONSTANT c_lf   = '\n'
05 CONSTANT c_crlf = '\r\n'

Examples

Example 1:

01 CONSTANT c1 ="Drink",   # Declares a STRING constant
02          c2 = 4711,     # Declares an INTEGER constant
03          n = 10,        # Declares an INTEGER constant
04          x SMALLINT = 1 # Declares a SMALLINT constant
05 DEFINE a ARRAY[n] OF INTEGER
06 MAIN
07   CONSTANT c1 = "Hello"
08   DEFINE i INTEGER
09   FOR i=1 TO n
10       ...
11   END FOR
12   DISPLAY c1 || c2  # Displays "Hello4711"
13 END MAIN