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
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
/* initialize scores to zero
*/
CALL check
/* call internal subroutine, check */
DO FOREVER
CALL throw response
/* call external subroutine, throw */
IFRESULT = 'machine' THEN /* the computer won
*/
computer = co 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
Writing a Subroutine;
76
z/OS V1R1.0 TSO/E REXX User’s Guide
Summary of Contents for TSO/E REXX
Page 1: ...z OS TSO E REXX User s Guide SA22 7791 00 ...
Page 2: ......
Page 3: ...z OS TSO E REXX User s Guide SA22 7791 00 ...
Page 10: ...viii z OS V1R1 0 TSO E REXX User s Guide ...
Page 12: ...x z OS V1R1 0 TSO E REXX User s Guide ...
Page 14: ...xii z OS V1R1 0 TSO E REXX User s Guide ...
Page 18: ...xvi z OS V1R1 0 TSO E REXX User s Guide ...
Page 20: ...2 z OS V1R1 0 TSO E REXX User s Guide ...
Page 58: ...Tracing Expressions with the TRACE Instruction 40 z OS V1R1 0 TSO E REXX User s Guide ...
Page 78: ...60 z OS V1R1 0 TSO E REXX User s Guide ...
Page 86: ...Built In Functions 68 z OS V1R1 0 TSO E REXX User s Guide ...
Page 128: ...Issuing Other Types of Commands from an Exec 110 z OS V1R1 0 TSO E REXX User s Guide ...
Page 136: ...Debugging Execs 118 z OS V1R1 0 TSO E REXX User s Guide ...
Page 170: ...Protecting Elements in the Data Stack 152 z OS V1R1 0 TSO E REXX User s Guide ...
Page 201: ...Part 3 Appendixes Copyright IBM Corp 1988 2001 183 ...
Page 202: ...184 z OS V1R1 0 TSO E REXX User s Guide ...
Page 222: ...Using Variables 204 z OS V1R1 0 TSO E REXX User s Guide ...
Page 226: ...208 z OS V1R1 0 TSO E REXX User s Guide ...
Page 238: ...220 z OS V1R1 0 TSO E REXX User s Guide ...
Page 241: ......