Writing A Function - IBM TSO/E REXX User Manual

Table of Contents

Advertisement

Writing a Function

A function is a series of instructions that an exec invokes to perform a specific task
and return a value. As was described in "Chapter 5. Using Functions" on page 61, a
function may be built-in or user-written. An exec invokes a user-written function the
same way it invokes a built-in function — by the function name immediately
Possible Solution (Internal Subroutine named CHECK)
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 the 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's not a valid response. Try again!"
SAY "Heads, tails, or quit?"
PULL response
END
END
RETURN
Possible Solution (External Subroutine named THROW)
/****************************** REXX *******************************/
/* This external subroutine receives the 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 different, the computer wins. The outcome is then */
/* returned to the calling exec.
/*******************************************************************/
ARG input
IF input = 'HEADS' THEN
userthrow = 0
ELSE
userthrow = 1
compthrow = RANDOM(0,1)
IF compthrow = userthrow THEN
outcome = 'human'
ELSE
outcome = 'machine'
RETURN outcome
/* heads = 0 */
/* tails = 1 */
/* choose a random number between */
/* 0 and 1 */
/* user chose correctly
/* user didn't choose correctly
Chapter 6. Writing Subroutines and Functions
Writing a Subroutine;
*/
*/
*/
*/
*/
*/
*/
*/
*/
77

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents