Using Recursion

Programming techniques are one of the areas that we want to cover in this monthly column. This is the first column devoted to this topic, so please let us know how you like it and if there are any specific areas you want covered.

When we we're working for IBM, we frequently received requests from users asking for the capability for RPG programs to be called recursively. In fact, it may surprise you to know that recursion was one of the most requested enhancements to the RPG language. With the introduction of subprocedures in V3R2, the RPG compiler team granted this request. Ever since V3R2 was announced however, we've been trying to find a simple way to demonstrate this capability to RPG programmers. We knew that recursion was a great technique for handling tasks such as parts explosions in bill of materials processing, but we struggled to produce a simple teaching example of the technique. Hopefully this one fits the bill and gets you thinking about different ways to program repetitive tasks.

Recursion is a program or procedure's capability to directly (or indirectly) call itself. Let's examine the use of recursion by comparing a recursive solution to a conventional coding approach to solve a problem. We might wish to replace the marker "&Name" with the actual Customer Name from the database. RPG IV provides the %Replace built-in-function (BIF) for this purpose. The following code snippet demonstrates the use of %Replace to achieve this.

D BaseText         S             200A    Varying
D PrintText        S             200A    Varying

C . . . . .    Eval       PrintText = %Replace(CustName : BaseText :
C . . . . .                           %Scan('&Name' : BaseText): 5)

This works well, but what if we have a longer piece of text (for example, a form letter) that may contain multiple instances of "&Name"? The conventional approach would be to use a DO loop to repeat the %Replace option until all instances of the marker have been replaced. Something like this:

* Prime the loop by locating the first replacement marker
C . . . .   Eval       Start = %Scan(&Name : BaseText)

* Keep going until no more markers to replace
C . . . .   DoW        Start > 0
C . . . .   Eval       PrintText = %Replace(CustName : BaseText :
C . . . . .                        %Scan('&Name' : BaseText): 5)
C . . . .   Eval       Start = %Scan(ScanFor:InText)
C . . . .   EndDo

This, or something similar, is probably the way you were taught to handle this kind of situation. (We're assuming that even if you were taught to use GOTO you've broken yourself of that nasty habit by now.) Recursion offers a different way of handling this problem--a different way, but not always a better way. For this particular example, in fact, it's probably overkill, but we want to introduce the technique in as simple a manner as possible.

















Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.

comments powered by Disqus



2017 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

The Microphone is Open

Add your voice: Should IBM i include open-source RPG tools?

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters