TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

Difference Between Functions & Subroutines.


Functions versus Subroutines:

Functions and Subroutines can be internal or external.

SubroutinesFunctions
Invoked by using the CALL instruction followed by the subroutine name and optionally up to 20 arguments Invoked by specifying the function's name immediately followed by parentheses that optionally contain up to 20 arguments.

Can be internal or external


Internal:

Can pass information by using common variables

Can protect variables with the PROCEDURE instruction

Can pass information by using arguments

External:

Must pass information by using arguments

Can use the ARG instruction or the ARG built-in function to receive arguments.

Can an be internal or external


Internal:

Can pass information by using common variables

Can protect variables with the PROCEDURE instruction

Can pass information by using arguments

External:

Must pass information by using arguments

Can use the ARG instruction or the ARG built-in function to receive arguments.

Might return a value to the caller. Must return a value to the caller.
Returns a value by placing it into the REXX special variable RESULT. Returns a value by replacing the function call with the value.
Uses the RETURN instruction to return to the caller. Uses the RETURN instruction to return to the caller.

Example 1:

Write an exec that plays a simulated coin toss game of heads or tails between the computer and a user and displays the accumulated scores. Start off with the message, "This is a game of chance. Type 'heads', 'tails', or 'quit' and press the Enter key."

There are four possible inputs namely, HEADS, TAILS, QUIT, None of these three (not valid response).

Write an internal subroutine without arguments to check for valid input. Send valid input to an external subroutine that compares the valid input with a random outcome. Use the RANDOM built- in function as, RANDOM (0, 1), and equate HEADS = 0, TAILS = 1. Return the result to the main program where results are tallied and displayed.

Code – Main Module:

/*******************************REXX **********************************/
/* This exec plays a simulated coin toss game between the computer    */
/* and a user. The user can enters one of the options namely          */
/* heads, tails, or quit. The user is first checked for validity in   */
/* an internal subroutine. An external subroutine uses the RANDOM     */
/* built-in function to obtain a simulation of a throw of dice and    */
/* compares the user input to the random outcome. The main exec       */
/* receives notification of who won the round. Scores are maintained  */
/* and displayed after each round.                                    */
/**********************************************************************/
SAY 'THIS IS A GAME OF CHANCE. TYPE "HEADS", "TAILS", OR "QUIT"
SAY ' AND PRESS ENTER.'
PULL RESPONSE

COMPUTER = 0                       /* INITIALIZE SCORES TO ZERO */
USER = 0
CALL CHECK                         /* CALL INTERNAL SUBROUTINE, CHECK */

DO FOREVER
    CALL THROW RESPONSE	           /* CALL EXTERNAL SUBROUTINE, THROW */
    IF RESULT = 'MACHINE' THEN	   /* THE COMPUTER WON */
        COMPUTER = COMPUTER + 1	   /* INCREASE THE COMPUTER SCORE */
    ELSE                           /* THE USER WON */
        USER = USER + 1	           /* INCREASE THE USER SCORE */
    SAY 'COMPUTER SCORE = ' COMPUTER ' YOUR SCORE = ' USER
    SAY 'HEADS, TAILS, OR QUIT?'
    PULL RESPONSE
    CALL CHECK	                   /* CALL INTERNAL SUBROUTINE, CHECK */
END
EXIT 0

CHECK:
/**********************************************************************/
/* This internal subroutine checks for valid input of “HEADS”, “TAILS”*/
/* or “QUIT”. If the user entered anything else, the subroutine tells */
/* the user that it is an invalid response and asks the user to try   */
/* again. The subroutine keeps repeating until user enters valid      */
/* input. Information is returned to the main exec through commonly   */
/* used variables                                                     */
/**********************************************************************/

DO UNTIL OUTCOME = 'CORRECT'
    SELECT
        WHEN RESPONSE = 'HEADS' THEN
              OUTCOME = 'CORRECT'
        WHEN RESPONSE = 'TAILS' THEN
              OUTCOME = 'CORRECT'
        WHEN RESPONSE = QUIT THEN
              EXIT
        OTHERWISE
              OUTCOME = 'INCORRECT'
              SAY 'THAT IS NOT A VALID RESPONSE. TRY AGAIN!'
              SAY 'HEADS, TAILS, OR QUIT'
              PULL RESPONSE
    END
END
RETURN

Code – External Subroutine:

/*******************************REXX *********************************/
/* This external subroutine receives valid input from the user       */
/* analyzes it, gets a random “throw” from the computer and compares */
/* the two values. If they are the same, the user wins. If they are  */
/* the same, the user wins. If they are different, computer wins.    */
/* The outcome is returned to the calling exec.                      */
/*********************************************************************/
ARG INPUT
IF INPUT = 'HEADS' THEN
    USERTHROW = 0                    /* HEADS = 0*/
ELSE
    USERTHROW = 1                    /* TAILS = 1*/

COMPTHROW = RANDOM(0,1)              /* CHOOSE A RANDOM NUMBER */
                                     /* BETWEEN 0 AND 1
IF COMPTHROW = USERTHROW THEN
    OUTCOME = 'HUMAN'                /* USER CHOSE CORRECTLY	*/
ELSE
    OUTCOME = 'MACHINE'              /* USER DIDN‟T CHOOSE CORRECTLY */

RETURN OUTCOME

Explanation:

  • Program receives HEADS, TAILS or QUIT input.

  • Internal subroutine CHECK verifies whether the user input is valid. In case of invalid inputs, user is prompted again to enter valid input.

  • Valid input is passed to an External subroutine THROW that compares the valid input with a random outcome.

  • RANDOM built-in function is used as, RANDOM(0,1), and equate HEADS = 0, TAILS= 1. External Subroutine returns the result to the main program where results are tallied and displayed.

  • Proper comments must be used for better understanding

  • Indentation must be appropriate for better readability


Example 2:

Write a function named "AVG" that receives a list of numbers separated by blanks, and computes their average as a decimal number. The function is called as follows:

AVG(number1 number2 number3 ...)

Code – Main module:

/*******************************REXX *********************************/
/* This exec receives three numbers from the user and call external  */
/* subroutine to calculate the average of three numbers              */
/*********************************************************************/
SAY 'ENTER FIRST NUMBER'
PULL FIRST
SAY 'ENTER SECOND NUMBER'
PULL SECOND
SAY 'ENTER THIRD NUMBER'
PULL THIRD
AVG = ELTAVG(FIRST,SECOND,THIRD)
SAY 'THE AVERAGE IS :'AVG

EXIT 0

Code - External Function:

/*******************************REXX *********************************/
/* This exec receives numbers, adds them, computers their average    */
/* and returns the average to the calling exec.                      */
/*********************************************************************/
ARG A, B, C
SUM = A+B+C
AVERAGE = SUM / 3
RETURN AVERAGE

Explanation:

  • This exec receives three numbers from the user and call external subroutine to calculate the average of the three numbers.

  • The external function ELTAVG receives numbers, adds them, computes their average and returns in to the calling exec.

  • Proper comments must be used for better understanding

  • Indentation must be appropriate for better readability


Summary:

  • A Subroutine is used as a common structure when the sequence of instructions is repeatedly used in a main program, which results in ease of use and understanding.

  • Functions are similar to subroutines except for the way in which they are called and the way the values are returned.

  • Functions are classified as user-defined functions and built-in functions.

  • The subroutines are broadly classified as internal and external subroutines.

  • Internal subroutines are coded with in the current exec and external subroutines are coded outside the exec and are accessible by other execs as well.

  • Internal subroutines can share the variables used by main exec where as external subroutines cannot.

  • Both external and internal subroutines can receive the information through arguments.


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal