Language basics / Constants |
The CONSTANT instruction defines a program constant.
[PRIVATE|PUBLIC] CONSTANT constant-definition [,...]
where constant-definition is:
identifier [ datatype] = literal
Constants define final static values that can be used in other instructions.
Constants can be defined with global, module, or function scope.
By default, module 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.
CONSTANT c1 = "Drink" -- Declares a STRING constant CONSTANT c2 = 4711 -- Declares an INTEGER constant
CONSTANT c1 SMALLINT = 12000 -- Would be an INTEGER by default
CONSTANT n = 10 DEFINE a ARRAY[n] OF INTEGER
CONSTANT n = 10 FOR i=1 TO n ...
Constants can be passed as function parameters, and returned from functions.
PUBLIC CONSTANT pi = 3.14159265
CONSTANT my_date DATE = MDY(12,24,2011) CONSTANT my_datetime DATETIME YEAR TO SECOND = DATETIME(2011-12-24 11:22:33) YEAR TO SECOND CONSTANT my_interval INTERVAL HOUR(5) TO FRACTION(3) = INTERVAL(-54351:50:24.234) HOUR(5) TO FRACTION(3)
CONSTANT pos = 3 -- Next line will produce an error at runtime SELECT * FROM customers ORDER BY pos
CONSTANT c1 CHAR(10) = "123" CONSTANT c2 CHAR(10) = "abc" DEFINE i INTEGER FOR i = 1 TO c1 -- Constant "123" is converted to 123 integer ... FOR i = 1 TO c2 -- Constant "abc" is converted to zero! ...
CONSTANT s CHAR(3) = "abcdef" DISPLAY s -- Displays "abc"
CONSTANT s CHAR(c) = "abc" -- Compiler error: c is not defined.
DEFINE c INTEGER CONSTANT s CHAR(c) = "abc" -- Compiler error: c is a variable, not a constant.
CONSTANT c INTEGER = 123 LET c = 345 -- Runtime error: c is a constant.
CONSTANT c CHAR(10) = "123" DEFINE s CHAR(c) -- Compiler error: c is a not an integer constant.
CONSTANT c_esc = '\x1b' CONSTANT c_tab = '\t' CONSTANT c_cr = '\r' CONSTANT c_lf = '\n' CONSTANT c_crlf = '\r\n'