Back to Contents
See also: Variables, Records, Data Types, User Types.
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:
01CONSTANT PI DECIMAL(12,10) = 3.1415926,
02MAX_SIZE INT = 10000,
03ERRMSG = "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.
[PRIVATE|PUBLIC] CONSTANT constant-definition [,...]
where constant-definition is:
identifier [ datatype ] = value
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:
01CONSTANT c1 = "Drink" -- Declares a STRING constant
02CONSTANT c2 = 4711 -- Declares an INTEGER constant
However, in some cases, you may need to specify the data type:
Constants can be used in variable, records, and array definitions:
01CONSTANT c1 SMALLINT = 12000 -- Would be an INTEGER by default
01CONSTANT n = 10
02DEFINE a ARRAY[n] OF INTEGER
Constants can be used at any place in the language where you normally use literals:
Constants can be passed as function parameters, and returned from functions.
01CONSTANT n = 10
02FOR i=1 TO n
Defining public constants in a module to be imported by others:
For date time constants, the value must be specified as an MDY(), DATETIME or INTERVAL literal:
01PUBLIC CONSTANT pi = 3.14159265
01CONSTANT my_date DATE = MDY(12,24,2011)
02CONSTANT my_datetime DATETIME YEAR TO SECOND = DATETIME(2011-12-24 11:22:33) YEAR TO SECOND
03CONSTANT 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:
01CONSTANT pos = 3
02SELECT * FROM customers ORDER BY pos
Automatic data type conversion can take place in some cases:
01CONSTANT c1 CHAR(10) = "123"
02CONSTANT c2 CHAR(10) = "abc"
03DEFINE i INTEGER
04FOR i = 1 TO c1 -- Constant "123" is converted to 123 integer
05FOR 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:
01CONSTANT s CHAR(3) = "abcdef"
02DISPLAY s -- Displays "abc"
The compiler throws an error when an undefined symbol is used in a constant declaration:
01CONSTANT s CHAR(c) = "abc" -- Compiler error: c is not defined.
The compiler throws an error when a variable is used in a constant declaration:
01DEFINE c INTEGER
02CONSTANT 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:
01CONSTANT c INTEGER = 123
02LET c = 345 -- Runtime error, c is a constant.
The compiler throws an error when the symbol used is not defined as an integer constant:
01CONSTANT c CHAR(10) = "123"
02DEFINE s CHAR(c) -- Compiler error, c is a not an integer constant.
You typically define common special characters with constants:
01CONSTANT c_esc = '\x1b'
02CONSTANT c_tab = '\t'
03CONSTANT c_cr = '\r'
04CONSTANT c_lf = '\n'
05CONSTANT c_crlf = '\r\n'
01CONSTANT c1 ="Drink", # Declares a STRING constant
02c2 = 4711, # Declares an INTEGER constant
03n = 10, # Declares an INTEGER constant
04x SMALLINT = 1 # Declares a SMALLINT constant
05DEFINE a ARRAY[n] OF INTEGER
07CONSTANT c1 = "Hello"
08DEFINE i INTEGER
09FOR i=1 TO n
12DISPLAY c1 || c2 # Displays "Hello4711"