Functions

A function is a sequence of instructions that can receive data, process it, and return a value. In REXX, there are several kinds of functions.

  • Built-in functions are built into the language processor. For more information, see Built-In functions.
  • User-written functions are those an individual user writes or an installation supplies. These can be internal or external. An internal function is part of the current program that starts at a label. An external function is a self-contained program or program outside the calling program. For information about user-written functions, see Writing subroutines and functions.
Regardless of the kind of function, all functions return a value to the program that issued the function call. To call a function, type the function name immediately followed by parentheses enclosing arguments to the function (if any). There can be no space between the function name and the left parenthesis.
function(arguments)
A function call can contain up to 20 arguments separated by commas. Arguments can be:
  • Constant
    function(55)
  • Symbol
    function(symbol_name)
  • Option that the function recognizes
    function(option)
  • Literal string
    function('With a literal string')
  • Unspecified or omitted
    function()
  • Another function
    function(function(arguments))
  • Combination of argument types
    function('With literal string', 55, option)
    function('With literal string',, option) /* Second argument
    omitted */
All functions must return values. When the function returns a value, the value replaces the function call. In the following example, the language processor adds the value the function returns to 7 and produces the sum.
SAY 7 + function(arguments)

A function call generally appears in an expression. Therefore a function call, like an expression, does not usually appear in an instruction by itself.

Example of a function

Calculations that functions represent often require many instructions. For example, the simple calculation for finding the highest number in a group of three numbers, might be written as follows:
Figure 1. Finding a Maximum Number

/***************************** REXX **********************************/
/* This program receives three numbers as arguments and analyzes     */
/* which number is the greatest.                                     */
/*********************************************************************/

PARSE ARG number1, number2, number3 .

IF number1 > number2 THEN
   IF number1 > number3 THEN
      greatest = number1
   ELSE
      greatest = number3
ELSE
   IF number2 > number3 THEN
      greatest = number2
   ELSE
      greatest = number3

RETURN greatest
Rather than writing multiple instructions every time you want to find the maximum of a group of three numbers, you can use a built-in function that does the calculation for you and returns the maximum number. The function is called MAX, and you can use it as follows:
MAX(number1,number2,number3,....)
To find the maximum of 45, -2, number , and 199 and put the maximum into the symbol biggest , write the following instruction:
biggest = MAX(45,-2,number,199)