Rexx - Functions



The code in Rexx is normally divided into Functions and Subroutines. Using functions helps in segregating the code into many more logical units. Let’s look at these functions in detail.

Defining a Function

The syntax of a function declaration is as follows −

FunctionName: 
PARSE ARG arguement1, arguement2… arguementN 
Return value 

Where,

  • FunctionName − This is the name assigned to the function.

  • PARSE ARG − These are keywords in Rexx which are used to mention that parameters are being passed onto the function.

  • arguement1, arguement2… arguementN − These are the arguments passed to the function.

  • Return value − This is the value returned by the function.

The following program is a simple example of how functions are used in Rexx.

/* Main program */ 
say add(5,6) 
exit 
add: 
PARSE ARG a,b 
return a + b 

The following things should be noted about the above program −

  • We are defining a function called add which accepts 2 parameters a and b.

  • The function uses the return statement to return the sum of a and b.

  • The exit statement has to be used to signify the end of the main program.

The output of the above program would be as follows −

11

Working with Arguments

In Rexx, there are specific functions which can be made to work with arguments. Let’s look at a couple of such arguments.

arg

This method is used to return the number of arguments defined for the function.

Syntax

arg() 

Parameters − None

Return Value − This method returns the number of arguments defined for the function.

Example

/* Main program */ 
say add(5,6) 
exit 
add: 
PARSE ARG a,b 

say arg() 
return a + b 

Output − When we run the above program we will get the following result.

2 
11 

arg(index)

This method is used to return the value of the argument at the specific position.

Syntax

arg(index)

Parameter

  • Index − Index position of the argument to be returned.

Return Value − This method returns the value of the argument at the specific position.

Example

/* Main program */ 
say add(5,6) 
exit 
add: 
PARSE ARG a,b 

say arg(1) 
return a + b 

Output − When we run the above program we will get the following result.

5 
11 

Recursive Functions

A recursive function or routine is one that calls itself. Any recursive function could be coded in a traditional non-recursive fashion (or iteratively), but sometimes recursion offers a better problem solution. Not all programming languages support recursion; Rexx does.

Let’s see an example of the famous factorial program using recursive functions in Rexx.

/* Main program */ 
do n = 1 to 5 
say 'The factorial of' n 'is:' factorial( n ) 
end 
return  

/* Function to get factorial */ 
factorial : procedure 
n = arg(1) 
if n = 1 then 
return 1 
return n * factorial( n - 1 ) 

The output of the above program is as follows −

The factorial of 1 is: 1
The factorial of 2 is: 2 
The factorial of 3 is: 6 
The factorial of 3 is: 24 
The factorial of 3 is: 120 
Advertisements