Subroutines and Functions

A subroutine is a series of instructions that a program calls to perform a specific task. The instruction that calls the subroutine is the CALL instruction. You can use the CALL instruction several times in a program to call 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.

dfhrx006

A function is a series of instructions that a program calls to perform a specific task and return a value. As Using Functions describes, a function can be built-in or user-written. Call a user-written function the same way as a built-in function: specify the function name immediately followed by parentheses that can contain arguments. There can be no blanks between the function name and the left parenthesis. The parentheses can contain up to 20 arguments or no arguments at all.
    function(argument1, argument2,…)
or
    function()
A function requires a return value because the function call generally appears in an expression.
z = function(arguments1, argument2,…)

When the function ends, it can use the RETURN instruction to send back a value to replace the function call.

dfhrx010

Both subroutines and functions can be internal (designated by a label) or external (designated by the subroutine or function in the REXX File System/VSE Librarian sublibrary member name). The two preceding examples illustrate an internal subroutine named sub1 and an internal function named func1.

Important: Because internal subroutines and functions generally appear after the main part of the program, when you have an internal subroutine or function, it is important to end the main part of the program with the EXIT instruction.

The following illustrates an external subroutine named sub2.

dfhrx007

The following illustrates an external function named func2.

dfhrx011


Concept Concept

Feedback


Timestamp icon Last updated: Tuesday, 7 January 2014


http://pic.dhe.ibm.com/infocenter/cicsts/v5r1/topic/com.ibm.cics.rexx.doc//dfhrx/wrsub.html