1
  1. I am having difficulty finding how recursion works. Some text books says that "Recursion is when a function calls itself again and again until some base condition is satisfied".

  2. Some books says that "Recursion is when a function calls another function again and again till some base condition is satisfied".

  3. Which is true? If both are true, can we consider below given example as a Recursion? If NO, then which is better in terms of performance,

    below code or recursion?


    def Function1()
    {
        /* do something */
    }

    def Function2()
    {
        for(i=0; i<=10; i++)
        {
             call Function1()
        }
    }
Adarsh Maurya
  • 348
  • 1
  • 4
  • 14
  • 1
    4. Neither are true. A number of functions may recursively call each other (mutual recursion). – EOF Jun 24 '17 at 11:07
  • 3
    It's hard to say. If `Function1` calls `Function2`, it's a recursion; otherwise, it is not a recursion. – Sergey Kalinichenko Jun 24 '17 at 11:08
  • I'd argue both definitions are too narrow. More generally, recursion is when an algorithm re-applies itself (e.g. recursive depth-first-search is performed by re-applying depth-first-search to each child of a parent node) - you don't technically need specific functions to achieve this. – Dai Jun 24 '17 at 11:15
  • @dai An algorithm doesn't necessarily cause recursion. Assuming you would write a new (identical) function for each recursion step in the algorithm that would not necessarily mean recursion on program level – tofro Jun 24 '17 at 11:20

6 Answers6

5

As it is, the code shows iterative calls to Function1(), if within the body of Function1() there is a call to Function2(), then it would be an indirect recursion - a function calling second function, which then calls the first again.

In general, a recursive function calls itself, either directly or indirectly. In direct recursion function, foo(), makes another call to itself. In indirect recursion, function foo() makes a call to function moo(), which in turn calls function foo(), until the base case is reached. (and then, the final result is accumulated in the exact reverse order of the initial recursive function call.)

Now, to answer your question:

Can this code be called Recursion. If not, which is more advanatgeous, given below code or recursion?

No. Iteration lacks multiple activation records (or stack frames), which makes it a better alternative.

OmG
  • 18,337
  • 10
  • 57
  • 90
Ziezi
  • 6,375
  • 3
  • 39
  • 49
  • If you have a decent compiler, tail-recursive functions don't use multiple activations records either. – EOF Jun 24 '17 at 11:29
3

Recursion should be used according to requirement and when you know the base condition and don't know how many times your loop should be called. Recursion can be of different types like there can be normal recursion, infinite recursion, indirect recursion etc. A normal recursion means calling the same function itself until a base condition is satisfied An infinite recursion is where there will not be any base condition. An indirect recursion is like when a calls b and b calls c and c calls back a then 'a' is being called indirectly.

Example of basic recursion:

foo() 
{
    base_condition;
        return;
    foo();
}
main()
{
    foo();
}

Your example base condition can be variable equals runs for 10 times.

fun(int x)
{
    if(x == 0)
        return;
    fun(--x);
}
main
{
    fun(10);
}

Please look into the below URL for performance criteria.

performance between looping and recursion

  • Recursion does not necessarily contain a base condition, infinite recursion is also a possibility. Also, a function does not need to call itself in order to be recursive. It can be indirectly recursive if it is called via a chain of other function calls, which at some point call the original function. – Lajos Arpad Jun 24 '17 at 12:47
  • Yes. I do agree, but a general recursion is called calling its own function. There can be different types of recursions also like tail recursion and infinite recursion as you said. – Susarla Nikhilesh Jun 24 '17 at 12:48
  • If a calls b call c calls a, then a is recursive, even though it is not calling itself. It is indirectly recursive, therefore your definition of "Recursion means calling the same function itself until a base condition is satisfied." is incomplete. – Lajos Arpad Jun 24 '17 at 12:50
  • Ok. Thanks for letting me know. :) – Susarla Nikhilesh Jun 24 '17 at 12:51
  • No problem. If you edit your answer and let me know about it, you will earn my upvote. – Lajos Arpad Jun 24 '17 at 12:54
  • It is good-enough, it already deserves the upvotes, however, instead of "normal recursion" I would have used "direct recursion". Also, infinite recursion is not necessarily different than direct or indirect recursion. A recursion can be infinite regardless of it being direct or indirect. But these are only nuances, I think your answer is already instructive to its readers. – Lajos Arpad Jun 24 '17 at 13:02
  • Thank you for letting me know. :) – Susarla Nikhilesh Jun 24 '17 at 13:03
  • You are welcome, keep up the constructive attitude and the good answers :) – Lajos Arpad Jun 24 '17 at 13:04
1

Let me put the pieces together: The code you provided could be recursive or it couldn't. This depends on the fact whether Function1 calls Function2. If this is the case, then you would have a mutual exclusion recursion, as pointed out by EOF.

However, this is a case that does not occur as often as normal recursion (except in pure functional environments). A normal recursion simply consists of a function which calls itself (insteaf of another function).

For further explanations and an introduction how, e. g., the recursive factorial works, see here.

Patrick
  • 184
  • 1
  • 1
  • 12
  • A function not necessarily calls itself in order to be recursive. In order for a function to be recursive you need to call it at least once more in order to evaluate it. If a calls b calls c calls d calls a, then a is recursive. – Lajos Arpad Jun 24 '17 at 12:46
1

Recursion is when

The control flow passes from a function into the very same function (directly or indirectly) repeatedly without returning first.

In your example, Function1is repeatedly called from Function2, but does return from each call before being called again, so it's not recursively called.

tofro
  • 5,640
  • 14
  • 31
0

Recursion is when a function calls itself, either directly or indirectly.

Normally in the C-like languages we use recursion only when the data is tree-like. For example, you have a tree consisting of nodes, with a "next" member to indicate siblings and a "child" member to indicate children (maybe it's an XML file, or a directory tree). You want the the number of nodes so

int getNodes(Node root)
{
    int answer = 1;
    for(sib = root.next; sib != null; sib = sib.next)
    {
       answer += 1;
       if(sib.child != null)
          answer += getNnodes(sib.child);
    }
    if(node.child != null)
       answer += getNodes(node.child);
    return answer;
}

You can make the code a bit neater but less efficient by return 0 if root == null.

However you can use recursion for iteration. Think of it as delivering letters to a street. You can go down the street removing letters from your sack (iteration). Or you can deliver letters to the first house in the street, declare the street to be one house shorter, and repeat until the street disappears (recursion). The latter seems eccentric, but it has some advantages for automatic checking of algorithm correctness. If you forget to increment it you can't get stuck in an infinite loop, for example.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18
0

Recursion occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics and computer science, where a function being defined is applied within its own definition. While this apparently defines an infinite number of instances (function values), it is often done in such a way that no loop or infinite chain of references can occur.

Source.

So, if you have a foo and in order to calculate or execute foo, you need to recur to foo at least once more, then you have a recursion. Example:

n! = 1 * 2 * 3 * ... * n

This is an iterative definition:

int fact(n) {
    int ret = 1;
    int i = 1;
    while (++i < n) ret *= i;
    return ret;
}

This is a recursive definition:

int fact(n) {
    return (n == 1) ? 1 : (n * fact(n - 1));
}

Since your code calculates Function2 with multiple usages of Function1, it is not recursive, since you did not need to call Function2 in order to evaluate Function2, nor did you need to call Function1 in order to evaluate Function1. Recursion occurs when something is its own dependency, while your code has a function which depends on another function. Your question about performance is next to impossible to answer, since there are infinite ways to implement a thing with or without recursion, when you compare a recursive approach with a non-recursive approach, then you need to have concrete implementations, or at least very strict ideas of how the two cases would be implemented. However, in general it is a good idea to prefer non-recursive approaches, as recursive approaches often have problems with the stack part of the memory, including stack overflow or infinite function calls and crashes due to bugs.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175