IBM SC34-5764-01 User Manual

Page 84

Advertising
background image

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
DO i = 1 TO 5

CALL subroutine
SAY answer

/* Produces 105 */

END
EXIT

subroutine:
DO i = 1 TO 5

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
DO i = 1 TO 5

SAY add()

/* Produces 105 */

END
EXIT

add:
DO i = 1 TO 5

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

Advertising