Jump to content

Rexx Programming/How to Rexx/function

From Wikibooks, open books for an open world

Function calls are followed by parentheses

[edit | edit source]

Function calls are followed by parentheses containing optional parameters to the function. A pair of empty parentheses are used if the function has no optional parameters.

Whitespace is not allowed between a function name and its argument list

[edit | edit source]

The parentheses containing function arguments must immediately follow the function name. No whitespace characters are allowed between the function name and its argument list, otherwise the function name may get misinterpreted as a variable name:

say random() /* No whitespace is allowed between the function and the parentheses */

Functions always return a result

[edit | edit source]

Functions in rexx always return a result which is substituted into the expression in place of the function call after the function has been executed. Some functions are already provided within Rexx for you to use, such as the RANDOM function used in the example above, or the ABS function which finds the absolute value of a number.

say abs(4 - 3 * (-5))  /* Absolute value of: 4 - 3 * (-5) --> replaced by 19 */
say 4 - 3 * abs(-5)    /* abs(-5) gets replaced by 5, final answer is -11 */

Different types of functions

[edit | edit source]

Rexx has built-in functions to process and return many different types of values, including numbers, Boolean values, or strings more generally. You have likely already seen some of them in different parts of this how-to.

say right(x, 5)           /* Right-justifies some text to a particular width. */
if datatype(x, 'N') then  /* Checks the data type of a given string. */
  x = abs(x)              /* Takes the absolute value of a number. */
say random()              /* Returns a random number. */

You can also create your own function by coding it as a subroutine, as described elsewhere.