Getting started with recursion

What can you learn here ?

Recursion coding techniques

When to use recursion

Dealing with tree structures

Recursion

Usually recursion is considered to be a rather advanced topic, but actually it's pretty straightforward - just easy to get it wrong. Object orientation and the increased use of classes and tree structures in everyday coding have made the use of recursion much easier to implement, and much more necessary to use.

What is recursion

Recursion is where a procedure calls itself repeatedly. Normally recursion can be mimicked by the use of loops, but this kind of looping coding can quickly become unintelligible when dealing with structures of indeterminate depth and complexity.

Replacing looping with recursion

One of the easiest ways of learning recursion concepts is to replace the function of a simple loop with a recursive function call. This does not mean that it is wise to implement loops this way, but simply that it is a straightforward way to 'get started'. Consider this loop:

Dim loops As Long

loops = 0

While Rnd() < 0.95

loops = loops + 1

Wend

MsgBox ("finished loop version " & CStr(loops))

This will count how many times a random number returns a value less that .95 and stop when > .95 is reached. Pretty pointless, but a good illustration of a loop where the upper limit is not known.

Now look at the same thing implemented as a recursive function. First the main code would look like this

MsgBox ("finished recursive version " & CStr(recurseTillRandom()))

Now the function

Function recurseTillRandom(Optional loops As Long = 0) As Long

If Rnd() < 0.95 Then loops = recurseTillRandom(loops + 1)

recurseTillRandom = loops

End Function

This will keep calling itself until a random number greater than .95 is reached, and the counting is done by calling itself with an ever increasing count. Eventually the condition is satisfied and the function returns the value of its argument. It's that simple. Make sure you get how this would play out before continuing. If necessary download the get started series workbook, set a breakpoint in the function, and step through it. Once you have it, you will have conquered the concept of recursion.

Navigating tree structures

One of the most common uses of recursion is to get around tree structures. You should take a look at Getting Started with Classes to see the details of the class we are going to use to illustrate. It looks like this

Option Explicit

Private pKey As Long

Private pName As String

Private pChildren As Collection

Public Property Get Key() As Long

Key = pKey

End Property

Public Property Get Name() As String

Name = pName

End Property

Public Property Get Children() As Collection

Set Children = pChildren

End Property

Public Property Let Key(p As Long)

pKey = p

End Property

Public Function Init(k As Long, sName As String) As cMyClass

pKey = k

pName = sName

Set pChildren = New Collection

Set Init = Me

End Function

The objective is to navigate through the children structure and print the details using recursion. Here is the test procedure

Summary

You can see from the above that navigating tree structures is ridiculously easy using recursion. Trying to implement navigation through such a structure using looping techniques rather than recursion would be unthinkable. There are many projects on this site that use recursion techniques. See here for more information. All code mentioned here is available here and you may leave comments or questions in our forum. For a real, but simple example of recursion, try How to traverse a treeview