Passing Information by Using Variables

When a program and its internal subroutine or function share the same variables, the value of a variable is what was last assigned. This is regardless of whether the assignment was in the main part of the program or in the subroutine or function.

The following example shows passing information to a subroutine. The variables number1, number2, and answer are shared. The value of answer is assigned in the subroutine and used in the main part of the program.
Figure 1. Example of Passing Information in a Variable Using a Subroutine
/******************************* REXX ********************************/
/*  This program receives a calculated value from an internal        */
/*  subroutine and uses that value in a SAY instruction.             */
/*********************************************************************/

  number1 = 5
  number2 = 10
  CALL subroutine
  SAY answer                       /* Produces 15 */
  EXIT

  subroutine:
  answer = number1 + number2
  RETURN
The next example is the same, except it passes information to a function rather than a subroutine. The subroutine includes the variable answer on the RETURN instruction. The language processor replaces the function call with the value in answer.
Figure 2. Example of Passing Information in a Variable Using a Function
/******************************* REXX ********************************/
/*  This program receives a calculated value from an internal        */
/*  function and uses SAY to produce that value.                     */
/*********************************************************************/

  number1 = 5
  number2 = 10
  SAY add()                          /* Produces 15 */
  SAY answer                         /* Also produces 15 */
  EXIT

  add:
  answer = number1 + number2
  RETURN answer
Using the same variables in a program and its internal subroutine or function can sometimes create problems. In the next example, the main part of the program and the subroutine use the same control variable, i, for their DO loops. As a result, the DO loop runs only once in the main program because the subroutine returns to the main program with i = 6.
Figure 3. 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 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
The next example is the same, except it passes information using a function instead of a subroutine.
Figure 4. Example of a Problem Caused by Passing Information in a Variable Using a Function
/******************************* 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
To avoid this kind of problem in an internal subroutine or function, you can use:
  • The PROCEDURE instruction, as the next topic describes.
  • 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 describes this.

Reference Reference

Feedback


Timestamp icon Last updated: Tuesday, 7 January 2014


http://pic.dhe.ibm.com/infocenter/cicsts/v5r1/topic/com.ibm.cics.rexx.doc//dfhrx/passvar.html