Summary:
See also: Variables, Arrays, Records, Constants, Programs
The GLOBALS instruction declares modular variables that can be exported to other program modules.
GLOBALS
declaration-statement
[,...]
END GLOBALS
GLOBALS
"filename"
If you modify filename, you must recompile all modules that include filename.
In general, a program variable is in scope only in the same FUNCTION, MAIN, or REPORT program block in which it was declared.
To extend the visibility of one or more module variables beyond the source module in which they are declared, you must take the following steps:
If a local variable has the same name as another variable that you declare in the GLOBALS statement, only the local variable is visible within its scope of reference.
Although you can include multiple GLOBALS…END GLOBALS statements in the same application, do not declare the same identifier as the name of a variable within the DEFINE statements of more than one GLOBALS declaration. Even if several declarations of a global variable defined in multiple places are identical, declaring any global variable more than once can result in compilation errors or unpredictable runtime behavior.
labels.4gl : This module defines the text that should be displayed on the screen
01
GLOBALS02
CONSTANT g_lbl_val = "Index:"03
CONSTANT g_lbl_idx = "Value:"04
END GLOBALS
globals.4gl : Declares a global array and a constant containing its size
01
GLOBALS "labels.4gl" -- this statement could be line 2 of main.4gl02
GLOBALS03
DEFINE g_idx ARRAY[100] OF CHAR(10)04
CONSTANT g_idxsize = 10005
END GLOBALS
database.4gl : This module could be dedicated to database access
01
GLOBALS "globals.4gl"02
FUNCTION get_id()03
DEFINE li INTEGER04
FOR li = 1 TO g_idxsize -- this could be a FOREACH statement05
LET g_idx[li] = g_idxsize - li06
END FOR07
END FUNCTION
main.4gl : Fill in the global array and display the result
01
GLOBALS "globals.4gl"02
MAIN03
DISPLAY "Initializing constant values for this application..."05
DISPLAY "Filling the data from function get_idx in module database.4gl..."06
CALL get_id()07
DISPLAY "Retrieving a few values from g_idx"08
CALL display_data()09
END MAIN10
FUNCTION display_data()11
DEFINE li INTEGER12
LET li = 113
WHILE li <= 10 AND li <= g_idxsize14
DISPLAY g_lbl_idx CLIPPED || li || " " || g_lbl_val CLIPPED || g_idx[li]15
LET li = li + 116
END WHILE17
END FUNCTION