1

I came across this code in a previous question :

a = 1

def func1():
    if a == 1:
        func2()

def func2():
    if a == 1:
        func3()

def func3():
    func1()

Is there ever a time when using recursion like this is more beneficial than a regular loop? If so, when should it be used and what is the convention?

tombam95
  • 343
  • 2
  • 14
  • 1
    This isn't really [recursion](http://programmers.stackexchange.com/questions/25052/in-plain-english-what-is-recursion), and there is nothing in this question that indicates any sort of "loop". This example simply compartementalizes some logical control flow statements within named functions. The purpose of this is usually for readability and ease of maintainability (i.e., by reducing the number of lines in code that need to be revised, should the underlying logic change) – David Zemens Nov 17 '15 at 13:21
  • 1
    @DavidZemens the example is mutually recursive, so it represents an infinite loop as is. – Aske Doerge Nov 17 '15 at 13:23

2 Answers2

3

There are uses for recursive calls and loopings:

When you use recursive:

  • Are quite simple logic call methods
  • Depend on how deeply you go in recursion you will have memory problems
  • They are made for use best on tree functions or methods

When you use looping

  • Are clean logic but not simple as recursive calls sometimes
  • You have better control on memory use instead of recursive methods or functions
  • They are made for use on iteration on list items.
  • 1
    The memory issue you mention is untrue. You just have to be careful and craft your functions to be tail-recursive: https://en.wikipedia.org/wiki/Tail_call – Aske Doerge Nov 17 '15 at 13:36
2

Both have their advantages and disadvantages which mainly depends on the programming language.

On a hardware level, Recursion comes with a cost, each time you call a function, the underlying mechanism has to store a pointer to where the program has to jump in the code after the function has finished. Among other things it also has to store the function arguments and local variable. This all is stored on the program Stack.

Some problems however have a much more natural solution while using recursion, for instance the tower of hanoi.

Readability is also an important consideration, to be honest I believe your example can benefit with loops.

An extensive comparison between iteration and recursion is given here. Which sums up the advantages and disadvantages.

DJanssens
  • 17,849
  • 7
  • 27
  • 42
  • 1
    Tail recursion solves the "store a pointer to where the program has to jump in the code after the function has finished.". This way the stack doesn't grow. Any resources not in the current function scope are freed. – Aske Doerge Nov 17 '15 at 13:40
  • I agree that there exist approaches that solve the drawback of recursion. Like I said it depends on the programming language and especially the underlying implementation of its constructs. I was just trying to give an overview of the traditional way recursion is handled. Tail recursion isn't always possible, and isn't implemented in all languages. But thank you for pointing it out. – DJanssens Nov 17 '15 at 13:44