Example - Writing An Internal And An External Subroutine - IBM TSO/E REXX User Manual

Table of Contents

Advertisement

Writing a Subroutine;

Example - Writing an Internal and an External Subroutine

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."
This means that there are four possible inputs:
v HEADS
v TAILS
v QUIT
v 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.
Good luck!
ANSWER
76
z/OS V1R1.0 TSO/E REXX User's Guide
Possible Solution (Main Exec)
/**************************** REXX *********************************/
/* This exec plays a simulated coin toss game between the computer */
/* and a user. The user enters heads, tails, or quit. The user
/* is first checked for validity in an internal subroutine.
/* An external subroutine uses the RANDOM build-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; user = 0
CALL check
DO FOREVER
CALL throw response
IF RESULT = 'machine' THEN /* the computer won
computer = computer + 1
ELSE
user = user + 1
SAY 'Computer score = ' computer
SAY 'Heads, tails, or quit?'
PULL response
CALL check
END
EXIT
/* initialize scores to zero
/* call internal subroutine, check */
/* call external subroutine, throw */
/* increase the computer score
/* the user won
/* increase the user score
'
Your score = ' user
/* call internal subroutine, check */
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents