z/OS TSO/E REXX User's Guide
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Passing Information by Using Variables

z/OS TSO/E REXX User's Guide
SA32-0982-00

When an exec and its internal function share the same variables, the value of a variable is what was last assigned, regardless of whether the assignment was in the main part of the exec or in the function. In the following example, the value of answer is assigned in the function and displayed in the main part of the exec. The variables number1, number2, and answer are shared. In addition, the value of answer replaces the function call because answer follows the RETURN instruction.

Example of Passing Information in a Variable

/****************************** REXX *******************************/
/* This exec receives a calculated value from an internal          */
/* function and displays that value.                               */
/*******************************************************************/

  number1 = 5
  number2 = 10
  SAY add()                                    /* Displays 15      */
  SAY answer                                   /* Also displays 15 */
  EXIT

  add:
  answer = number1 + number2
  RETURN answer

Using the same variables in an exec and its internal function can sometimes create problems. In the following example, the main part of the exec and the function use the same control variable, "i", for their DO loops. As a result, the DO loop repeats only once in the main exec because the function returns to the main exec with i = 6.

Example of a Problem Caused by Passing Information in a Variable

/****************************** REXX *******************************/
/* This exec uses an instruction in a DO loop to call an internal  */
/* function.  A problem occurs because the function also uses a DO */
/* loop with the same control variable as the main exec.  The DO   */
/* loop in the main exec repeats only once.                        */
/*******************************************************************/

  number1 = 5
  number2 = 10
  DO i = 1 TO 5
    SAY add()                                      /* Displays 105 */
  END
  EXIT

  add:
  DO i = 1 TO 5
    answer = number1 + number2
    number1 = number2
    number2 = answer
  END
  RETURN answer
To avoid this kind of problem in an internal function, you can use:
  • The PROCEDURE instruction as described in the next topic.
  • Different variable names in a function.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014