Back to Contents


Globals

Summary:

See also: Variables, Arrays, Records, Constants, Programs


Definition

Purpose:

The GLOBALS instruction declares modular variables that can be exported to other program modules.

Syntax 1: Global block declaration

GLOBALS
  declaration-statement
 
[,...]
END GLOBALS

Syntax 2: Importing global variables

GLOBALS "filename"

Notes:

  1. In Syntax 1, declaration-statement is a variable or constant declaration.
  2. Each variable declared in a GLOBALS ... END GLOBALS block is a global variable.
  3. A GLOBALS file does not contain any executable statement.
  4. You do not compile the source file containing the GLOBALS block.
  5. In Syntax 2filename is the name of a file containing the definition of global variables. Use this syntax to include a global declarations in the current module.

Tips:

  1. It is possible to use the GLOBALS "filename" instruction inside a GLOBALS file.
  2. It is possible to declare several GLOBALS "filename" in a module.
  3. There is no need to compile filename, but compiling filename might be useful to detect syntax errors.
  4. To improve the readability of your source code, prefix global variables by "g_".
  5. Global variables are often used as constants.
  6. Global arrays allow a function to access the array modified by another function.

Warnings:

  1. If you modify filename, you must recompile all modules that include filename.

  2. Do not declare a variable outside a GLOBALSEND GLOBALS block in a GLOBALS file.
  3. Avoid confusing function names and global variables names.
  4. Avoid declaring the same global variable twice when including multiple GLOBALS files.

Usage:

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:

  1. Declare variables in GLOBALSEND GLOBALS declarations in files containing only GLOBALS, DEFINE, and DATABASE statements (but no executable statements).
  2. Specify the files in GLOBALS "filename" statements in each additional source module that includes statements referencing the variables.

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 GLOBALSEND 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.


Examples

Example 1: Multiple GLOBALS file

labels.4gl : This module defines the text that should be displayed on the screen

01 GLOBALS
02   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.4gl                                                                                                                                    
02 GLOBALS
03   DEFINE g_idx ARRAY[100] OF CHAR(10)
04   CONSTANT g_idxsize = 100
05 END GLOBALS

database.4gl : This module could be dedicated to database access

01 GLOBALS "globals.4gl"
02 FUNCTION get_id()
03   DEFINE li INTEGER
04   FOR li = 1 TO g_idxsize -- this could be a FOREACH statement
05       LET g_idx[li] = g_idxsize - li
06   END FOR
07 END FUNCTION

main.4gl : Fill in the global array and display the result

01 GLOBALS "globals.4gl"
02 MAIN
03   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 MAIN                                                                                                                                    
10 FUNCTION display_data()
11   DEFINE li INTEGER
12   LET li = 1
13   WHILE li <= 10 AND li <= g_idxsize
14       DISPLAY g_lbl_idx CLIPPED || li || " " || g_lbl_val CLIPPED || g_idx[li]
15       LET li = li + 1
16   END WHILE
17 END FUNCTION