Internet
Fact-checked

At EasyTechJunkie, we're committed to delivering accurate, trustworthy information. Our expert-authored content is rigorously fact-checked and sourced from credible authorities. Discover how we uphold the highest standards in providing you with reliable knowledge.

Learn more...

What Is a Recursive Call?

Franklin Jeffrey
Franklin Jeffrey

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.

Man holding computer
Man holding computer

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.

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.

Discuss this Article

Post your comments
Login:
Forgot password?
Register:
    • Man holding computer
      Man holding computer