In addition to task variables, there are “procedure-local” variables with the scopes PROCEDURE and CURRENT. For a procedure called with CALL-PROCEDURE, these are identical. In contrast to this, an “include procedure”, which can access the variables of the calling procedure without declaring them again or importing them, can select whether a declaration is to be effective within the scope of the caller (SCOPE=*PROCEDURE) or is to be “private”, i.e to be valid only within the include procedure itself (SCOPE=*CURRENT).
If a variable is declared implicitly, it always receives the scope *CURRENT.
Importing a procedure-local variable
A called procedure can import a procedure-local variable provided the variable has been declared in the calling procedure by means of the following command:
/DECLARE-VARIABLE VAR-NAME=...,SCOPE=*CURRENT/*PROCEDURE(IMPORT-ALLOWED=*YES)
and the called procedure uses the following command to import the variable:
/IMPORT-VARIABLE NAME=..., FROM=*SCOPE(SCOPE==*CALLING-PROCEDURES)
Example
Include procedures which use the caller’s variables can be kept very simple, since it is often unnecessary to declare parameters and local variables. The following sample procedure calls an include procedure several times in order to calculate the VAT for various amounts of money and to send this VAT value to SYSOUT.
Calling procedure:
/ “Calculation of VAT” / / “Defaults” / DECIMAL-CHAR = '.'; VAT-RATE = 16 “%” / / “1st calculation” / AMOUNT = 5730 / INCLUDE-PROCEDURE I.VAT / / “2nd calculation” / AMOUNT = 9820 * 3 / INCLUDE-PROCEDURE I.VAT
Called procedure I.VAT:
/ “Calculate the tax” / VATAX = (AMOUNT * VAT-RATE + 50) / 100 / / “Output the tax” / PENCE-OUT = STRING(VATAX) / PENCE-LGTH = LENGTH(PENCE-OUT) / POUND-OUT = SUBSTR(PENCE-OUT,1,PENCE-LGTH - 2) - / // DECIMAL-CHAR - / // SUBSTR(PENCE-OUT,PENCE-LGTH - 1) / WRITE-TEXT 'The tax is &POUND-OUT Pounds'
Placing the above commands in an include procedure saves calculating the tax at several different positions in the first procedure. The include procedure can access the variables DECIMAL-CHAR, AMOUNT and VAT-RATE without having to pass these as parameters.
If variable are declared within an include procedure, this procedure can decide whether these variables are to be visible in the calling procedure. Such visibility is particularly useful if the main purpose of the include call is to execute declarations.
Example
The procedure:
/DECLARE-VARIABLE NAME(TYPE=*STRING),SCOPE=*PROCEDURE /DECLARE-VARIABLE COUNTRY(TYPE=*STRING,INIT='D'),SCOPE=*PROCEDURE /DECLARE-VARIABLE ZIP(TYPE=*INTEGER),SCOPE=*PROCEDURE /DECLARE-VARIABLE CITY(TYPE=*STRING),SCOPE=*PROCEDURE
can be called with /INCLUDE-PROCEDURE from several procedures in order to ensure compatible declarations for the variables NAME, COUNTRY, ZIP and CITY. If these declarations need to be modified later, it is sufficient to modify them centrally in the include procedure.
In this case, SCOPE=*PROCEDURE is specified to make the declarations effective in the procedure scope of the calling procedure and to ensure that they remain active after termination of the include procedure.
Declarations with SCOPE=*CURRENT, in contrast, are local to the include procedure. This can be used to avoid conflicts with variable definitions in the calling procedure.
Example
If the following declaration is included at the beginning of the sample procedure I.VAT on page 160:
/DECL-VARIABLE (VATAX, PENCE-OUT, PENCE-LGTH, POUND-OUT)
then the calling procedure could use a variable with one of these names without this variable being changed by the include call (SCOPE=*CURRENT is the default and does not, therefore, need to be specified in the DECLARE-VARIABLE command).
Procedure interruption
If a procedure is interrupted with the HOLD-PROCEDURE command or with the
key, all procedure-local variables are accessible in interactive mode, i.e. the interruption dialog belongs to the visibility scope of the interrupted procedure.Variables which are declared during the interruption dialog are subsequently regarded as procedure-local variables of the interrupted procedure, regardless of whether this is a call procedure or an include procedure.