Declaration context

The DEFINE statement declares the identifier of one or more variables, that will be visible to other program blocks according to the declaration context of the variables. The scope of reference of a variable defines where it can be referenced in the program. According to the location of the variable definition, memory will be allocated when the program starts, or during the program execution.

The context of a variable declaration in the source module determines where a variable can be referenced by other language statements, and when storage is allocated for the variable in memory. The DEFINE statement can appear in three contexts:

  1. Within a FUNCTION, MAIN, or REPORT program block, DEFINE declares local variables, and causes memory to be allocated on the runtime stack when the function is called. These DEFINE declarations of local variables must precede any procedural statements within the same program block. The scope of reference of a local variable is restricted to the same program block. The variable is not visible elsewhere. Functions can be called recursively, and each recursive entry creates its own set of local variables. The variable is unique to that invocation of its program block. Each time the block is entered, a new copy of the variable is created.
  2. Outside any FUNCTION, REPORT, or MAIN program block, the DEFINE statement declares module variables, and causes storage to be allocated for them at program startup. These declarations must appear before any program blocks. Scope of reference is from the DEFINE statement to the end of the same module. Module variables are be default private to the module, but can become visible to other modules when declared with the PUBLIC qualifier.
  3. Inside a GLOBALS block, the DEFINE statement declares global variables that are visible to the whole program. The memory for global variables is allocated when the program starts. Multiple GLOBALS blocks can be defined for a given module. Use one module to declare all global variables and reference that module within other modules by using the GLOBALS "filename.4gl" statement as the first statement in the module, outside any program block.

A compile-time error occurs if you declare the same name for two variables that have the same scope. You can, however, declare the same name for variables that differ in their scope. For example, you can use the same identifier to reference different local variables in different program blocks.

You can also declare the same name for two or more variables whose scopes of reference are different but overlapping. Within their intersection, the compiler interprets the identifier as referencing the variable whose scope is smaller, and therefore the variable whose scope is a superset of the other is not visible.

If a local variable has the same identifier as a global variable, then the local variable takes precedence inside the program block in which it is declared. Elsewhere in the program, the identifier references the global variable.

A module variable can have the same name as a global variable that is declared in a different module. Within the module where the module variable is declared, the module variable takes precedence over the global variable. Statements in that module cannot reference the global variable.

A module variable cannot have the same name as a global variable that is declared in the same module.

If a local variable has the same identifier as a module variable, then the local identifier takes precedence inside the program block in which it is declared. Elsewhere in the same source-code module, the name references the module variable.