The next example is the same, except it passes information using a function instead of a subroutine.
To avoid this kind of problem in an internal subroutine or function, you can use:
v The PROCEDURE instruction, as the next topic describes.
v Different variable names in a subroutine or function than in the main part of the program. For a
subroutine, you can pass arguments on the CALL instruction; section “Passing Information by Using
Arguments” on page 64 describes this.
Protecting Variables with the PROCEDURE Instruction: When you use the PROCEDURE instruction
immediately after the subroutine or function label, all variables in the subroutine or function become local
to the subroutine or function; they are shielded from the main part of the program. You can also use the
PROCEDURE EXPOSE instruction to protect all but a few specified variables.
/******************************* REXX ********************************/
/* NOTE: This program contains an error. */
/* It uses a DO loop to call an internal subroutine, and the */
/* subroutine uses a DO loop with the same control variable as the */
/* main program. The DO loop in the main program runs only once. */
/*********************************************************************/
number1 = 5
number2 = 10
DOi=1TO5
CALL subroutine
SAY answer /* Produces 105 */
END
EXIT
subroutine:
DOi=1TO5
answer = number1 + number2
number1 = number2
number2 = answer
END
RETURN
Figure 31. Example of a Problem Caused by Passing Information in a Variable Using a Subroutine
/******************************* REXX ********************************/
/* NOTE: This program contains an error. */
/* It uses a DO loop to call an internal function, and the */
/* function uses a DO loop with the same control variable as the */
/* main program. The DO loop in the main program runs only once. */
/*********************************************************************/
number1 = 5
number2 = 10
DOi=1TO5
SAY add() /* Produces 105 */
END
EXIT
add:
DOi=1TO5
answer = number1 + number2
number1 = number2
number2 = answer
END
RETURN answer
Figure 32. Example of a Problem Caused by Passing Information in a Variable Using a Function
Writing Subroutines and Functions
62
CICS TS for VSE/ESA: REXX Guide