Recursive call in RPG ILE

A previous tip stated that RPG does not allow recursive calls. Actually ILE RPG does allow recursion. A program cannot call itself, but it can call a subprocedure that calls itself.

The procedure call should not use the CALLP opcode, rather it should use the EVAL opcode and treat the procedure as a function. For example, a procedure name CALCS would use the following code:


The sample code calculates the n'th number in a Fibonacci sequence. A Fibonacci sequence is a sequence whereby any number is equal to the previous two numbers in the sequence:

Nbr(n) = Nbr(n-1) + Nbr(n-2).

In the sequence, the first number is 1, preceeded by an implied zero so that the second number has an "n-2" to use.

The first few elements then calculate to:
1 1 2 3 5 8 13 21 34

It is an interesting exercise to run this procedure in DEBUG and observe its action.

The subprocedure CALCS would be called from an ILE RPG program using a statement such as:


Note that if the recursion proceeds through too many recursive calls, that performance drastically slows down. In this example, anything over 30 calls really ran slowly. If the job ends abnormally while into a deep call stack, the end job process takes a very long time. Because of this, I cannot think of many practical applications of recursion in ILE RPG.

   * Procedure Prototype
DCALCS          PR             9P 0    
D                              9P 0    
 * * * * * * * * * * * * * * * * * *  * Procedure Definition
P CALCS         B
*                                       D CALCS         PI             9P 0
D  NBR                         9P 0 
* Procedure variables 
D NM1             S            9P 0
D NM2             S            9P 0
*                                       C                   SELECT                    
 * Endpoint if inbound parm = 0
C      WHEN      NBR = 0         
C      RETURN    1               
 * Endpoint if inbound parm = 1
C      WHEN      NBR = 1         
C      RETURN    1               
 * Endpoint if inbound parm = 2
C      WHEN      NBR = 2         
C      RETURN    1               
 * Recursive call
C      OTHER
C      EVAL      NM1 = NBR - 1   
C      EVAL      NM2 = NBR - 2   


The Best Web Links: tips, tutorials and more.

Ask your programming questions--or help out your peers by answering them--in our live discussion forums.

Ask the Experts yourself: Our application development gurus are waiting to answer your programming questions.

Search400's targeted search engine: Get relevant information on RPG.

This was first published in September 2001

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.