Problem:
Method to pass variables between programs. Correct syntax for passing a variable back to the calling program from the EXIT PROGRAM RETURNING ... statement.
Resolution:
First Method - returning a value from the EXIT PROGRAM statement in the called program:
Pass the value of an alphanumeric variable from the called program in the RETURNING clause of the EXIT PROGRAM statement. The variables are defined in the working-storage section of both programs.
The call to the called program is
call prog2 returning ws-var1.
The correct syntax of the called program (prog2) would be:
procedure division returning ws-var1.
...
exit program returning ws-var1.
Returning a variable with this method (from the EXIT PROGRAM statement) returns the 'value' of the variable. On a 32-bit machine, the length of the value is limited to 4 characters.
Second Method - declaring and using a 'Linkage section' data item in the called program to point to the variable passed by the calling program:
The recommended way to pass variables between programs is with the USING clause and declaring a variable in the Linkage Section of the called program. With this method of passing variables, the address is passed from the calling program to the called program. The called program updates the value at the memory address of the data item defined in the calling program.
In the calling program the variable is defined in the working-storage section as an alphanumeric. If the variable is named 'ws-myvar', the call to the second program would be:
Call prog2 using ws-myvar.
In the called program, the variable that corresponds to the same memory address is declared in the Linkage section with the same data type and length as the corresponding variable in the calling program. The variable is specified in the 'using' clause of the procedure division:
Linkage section.
01 ls-myvar pic x(9).
Procedure division using ls-myvar.
Move "some text" to ls-myvar.
The value is stored and the calling program uses the corresponding working-storage data item to access the value at that memory address.
The two demos demonstrating either method is attached to this KB article.
- 'returning' demo - demonstrates passing the 'value' of a variable to the calling program. Change the animate settings so that the startup program is Prog1error to see that a maximum of 4 characters are passed to the calling program.
- 'demoSubprog' demo - demonstrates the use of the Linkage section in the called program to update the value at the address of the variable defined in the calling program.


