Using Functions

Rexx functions can be used in any expression. In the following example, the built-in function WORD is used to return the third blank-delimited word in a string:

/* Example of function use                                     */
myname="John Q. Public"   /* assign a literal string to MYNAME */
surname=word(myname,3)    /* assign WORD result to SURNAME     */
say surname               /* display Public                    */

Literal strings can be supplied as arguments to functions, so the previous program can be rewritten as follows:

/* Example of function use                                            */
surname=word("John Q. Public",3) /* assign WORD result to SURNAME     */
say surname                      /* display Public                    */

Because an expression can be used with the SAY instruction, you can further reduce the program to:

/* Example of function use                                            */
say word("John Q. Public",3)

Functions can be nested. Suppose you want to display only the first two letters of the third word, Public. The LEFT function can return the first two letters, but you need to give it the third word. LEFT expects the input string as its first argument and the number of characters to return as its second argument:

/* Example of function use */

/* Here is how to do it without nesting */
thirdword=word("John Q. Public",3)
say left(thirdword,2)

/* And here is how to do it with nesting */
say left(word("John Q. Public",3),2)