A subroutine does not have to return a value, but when it does, it sends back the
value with the RETURN instruction.
RETURN value
The calling exec receives the value in the REXX special variable named
RESULT.
SAY 'The answer is' RESULT
v Returning a value from a function
A function must return a value. When the function is a REXX exec, the value is
returned with either the RETURN or EXIT instruction.
RETURN value
The calling exec receives the value at the function call. The value replaces the
function call, so that in the following example, x = value.
x = function(argument1, argument2,...)
When to Write Subroutines vs. Functions
The actual instructions that make up a subroutine or a function can be identical. It is
the way you want to use them in an exec that turns them into either a subroutine or
a function. For example, the built-in function SUBSTR can be called as either a
function or a subroutine. As a function, you invoke it as follows to shorten a word to
its first eight characters:
x = SUBSTR('verylongword',1,8) /* x is set to 'verylong' */
As a subroutine, you would get the same results with the following instructions:
CALL SUBSTR 'verylongword', 1, 8 /* x is set to 'verylong' */
x = RESULT
When deciding whether to write a subroutine or a function, ask yourself the
following questions:
v Is a returned value optional? If so, write a subroutine.
v Do I need a value returned as an expression within an instruction? If so, write a
function.
The rest of this chapter describes how to write subroutines, how to write functions,
and finally summarizes the differences and similarities between the two.
Writing a Subroutine
A subroutine is a series of instructions that an exec invokes to perform a specific
task. The instruction that invokes the subroutine is the CALL instruction. The CALL
instruction may be used several times in an exec to invoke the same subroutine.
When the subroutine ends, it can return control to the instruction that directly
follows the subroutine call. The instruction that returns control is the RETURN
instruction.
What are Subroutines and Functions?
70
z/OS V1R1.0 TSO/E REXX User’s Guide