Task variables can be addressed from within a procedure only if this is explicitly permitted. This can be done in three possible ways:
Declaration of a task variable
The /DECLARE-VARIABLE ...,SCOPE=*TASK command can be used to create a task variable; after this, the variable can be addressed with the specified name in the procedure which declared it.
- If a task variable with the specified name already exists, the DECLARE-VARIABLE command makes it accessible; note, however, that the other operands in the command may not conflict with the attributes of the existing variable.
- Importing a task variable
If a procedure is to access a task variable which is known to exist, it is sufficient to include an IMPORT-VARIABLE command for this variable in the procedure.
Importing a variable is equivalent to declaring it with a DECLARE-VARIABLE command with the specification SCOPE=*TASK, but it is not possible to create a new variable. On the other hand, it saves the effort of ensuring that the attributes TYPE and MULTIPLE-ELEMENTS (including any existing substructures) specified in DECLARE-VARIABLE do not conflict with the existing values.Example
A task variable WORKLIBRARY is to be created and initialized only if it does not yet exist; otherwise, the current procedure is to use the existing definition:
/IF IS-DECLARED('WORKLIBRARY',SCOPE=*TASK) / IMPORT-VARIABLE WORKLIBRARY “The variable may have any data type” /ELSE / DECLARE-VARIABLE WORKLIBRARY(INITIAL-VALUE='#WORKLIB',TYPE=*STRING) /END-IF
Using a task variable as a container
The two possibilities described above permit a task variable to be used only under its original name from within a procedure. If this is not desired because, for example, a local variable with the same name exists in the procedure or the name of the task variable is too long and too complex (due to the requirement for unique names within a task), a task variable can be used as a “container” for a variable created within the procedure.
Example
/DECLARE-VARIABLE I(INIT-VALUE=0,TYPE=*INTEGER), - / CONTAINER=*VAR(ZAEHLER-FUER-PROZ-A,SCOPE=*TASK)
After this declaration, commands such as
/I = I + 1can be used to access the task variable, whose actual name (COUNTER-FOR-PROC-A) avoids conflicts with the names of other task variables.
The task variable used as a container must already exist. For this reason, the attributes of the variable which refers to the variable container may not conflict with the declaration of the variable container (TYPE, DEFINITION, MULTIPLE-ELEMENTS).
Procedure interruption
When a procedure is interrupted, only those variables which are visible within the currently interrupted procedure remain visible.