Summary:
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:
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.
[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:
01
CONSTANT c1 = "Drink" -- Declares a STRING constant02
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 = 1002
DEFINE 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.01
CONSTANT n = 1002
FOR i=1 TO n
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 SECOND03
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 = 302
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 INTEGER04
FOR i = 1 TO c1 -- Constant "123" is converted to 123 integer05
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 INTEGER02
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 = 12302
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'
01
CONSTANT c1 ="Drink", # Declares a STRING constant02
c2 = 4711, # Declares an INTEGER constant03
n = 10, # Declares an INTEGER constant04
x SMALLINT = 1 # Declares a SMALLINT constant05
DEFINE a ARRAY[n] OF INTEGER06
MAIN07
CONSTANT c1 = "Hello"08
DEFINE i INTEGER09
FOR i=1 TO n10
...11
END FOR12
DISPLAY c1 || c2 # Displays "Hello4711"13
END MAIN