Category: 

What Is a Recursive Call?

Article Details
  • Written By: Franklin Jeffrey
  • Edited By: Rachel Catherine Allen
  • Last Modified Date: 24 November 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
Snake charmers get snakes to “dance” because of the movement of their flute-like instruments, not their music.  more...

December 4 ,  1945 :  The United States Senate approved of US participation in the United Nations.  more...

In programming, a recursive call is a command within a subroutine or function that tells the program to run the same subroutine again. The repeat performance may be the direct result of the function, or a second function may be triggered that, in turn, refers back to the first function. A recursive call has some similarities to the dreaded infinite loop, but the subroutine always has a conditional statement that tells the program when to stop repeating the recursion.

The concept of recursion is perhaps best illustrated through the use of an example. Suppose a roofer is applying new shingles to a home. To begin, he must carry a bundle of shingles to the roof. Once he has nailed the first bundle in place, he must climb down the ladder, retrieve another bundle and nail it in place. The process continues as a series of "go, fetch, return" until the last shingle has been applied. At that point, the roofer is free to move on to the next job or go home.

Although the example is an oversimplification, it contains all of the elements of a recursive call. There is a starting point, the roofer must retrieve what he needs, return to the beginning and, when the final condition is met, stop. This is basically what the program does; it starts, implements an action, returns to itself and terminates when the ending condition occurs.

Ad

The ending condition is referred to as the base case. It is essential for all recursive calls; without it, the function would continue to repeat. At best, this results in draining the system's memory resources. Normally the overload will crash the program at some point, but by the time the problem is discovered, significant damage may be done.

Experienced programmers might recognize the similarity between a recursive call and a "for" or "while" loop. If, for example, the goal is to find the total inventory count of all stock with part numbers greater than 999, a "for" loop tells the program to locate all qualifying instances and a "while" loop tells the program to execute the loop only while the stated condition is valid. A recursive call might be said to combine some of the features of these loops with an "if-then-else" statement; if this condition is true, then do this, or else do something different if the condition is false. Recursion typically allows for more compact code, however, and allows the problem to be passed to the function nearer the point it is needed.

Ad

You might also Like

Recommended

Discuss this Article

Post your comments

Post Anonymously

Login

username
password
forgot password?

Register

username
password
confirm
email