-2

I was going through this recursion program in cpp on w3schools.

#include <iostream>
using namespace std;

int sum(int k) {
  if (k > 0) {
    return k + sum(k - 1);
  } else {
    return 0;
  }
}

int main() {
  int result = sum(10);
  cout << result;
  return 0;
}

There is a condition block in the recursive function above. Assuming that it will eventually go to the 'else' block, the value of 0 will be returned from the function. However, it's confusing to me how the function call sum(10) is working properly and not just returning 0.

Could someone explain the recursion to me.

Yone
  • 867
  • 1
  • 7
  • 22
  • 2
    It returns `0` as result. Sorry, I don't understand the question, what else would you expect? – πάντα ῥεῖ Jun 17 '23 at 06:38
  • 1
    `return` returns only from one level of nested call, not all of them. So it does return the actual 0, but it gives it not to `main`, but to `sum(1)`. – HolyBlackCat Jun 17 '23 at 06:39
  • `return` with our without value, will leave the function even if it is placed somewhere in the middle of the function. In your case it is also the stop condition of your recursion. Side note : stop using `using namespace std;` – Pepijn Kramer Jun 17 '23 at 06:40
  • A recursive function call is just like any other function call. If you call a function `a` which calls a function `b`, and `b` have `return 0;`, would you still ask the same question? – Some programmer dude Jun 17 '23 at 06:40
  • As for your specific example, there's not only a `return 0` statement in your function. Don't forget the `return k + sum(k - 1)` statement. – Some programmer dude Jun 17 '23 at 06:41
  • `return` does not mean 'return to main', it means return to the calling function. In a recursive function the calling function can be the same function. So in this case the `return 0;` returns back to the `sum` function not to the `main` function. – john Jun 17 '23 at 06:49
  • Does this answer your question? [How Recursion works in C](https://stackoverflow.com/questions/5631447/how-recursion-works-in-c) – JaMiT Jun 17 '23 at 06:50
  • What's interesting to the compiler is the last `return` statement executed, not the first one. In your case there are 11 calls of `sum` on the stack when `return 0` happens and the `return` in the 11th call passes the value on to the 10th call; this value is then used for calculating the return value of the 10th call, which returns a different value. This repeats until you get to the 1st call of the function communicating the final value back to `main` and that's the only value `main` gets to know about. With `return` information is always passed exactly 1 level up the call stack... – fabian Jun 17 '23 at 06:51
  • 1
    `return` always means return to the calling function. In your example the function `sum` is going to be called 10 times because of the recursion, so you also need to return 10 times to get back to `main`. Maybe this would be easier to understand if you used a smaller number like 2 or 3, instead of 10. – john Jun 17 '23 at 06:53
  • @SANKET I think your main problem is that recursive code like that is hard to read, since it doesn't give you an obvious view in which order the return statements are applied while iterations run. Prefer to use a simple loop for doing such. And regarding those _"variables"_ you're missing: There's a call stack, which holds those values. – πάντα ῥεῖ Jun 17 '23 at 07:10
  • 1
    Voted to reopen. The question is clear; it even has a correct and accepted answer. – Pete Becker Jun 17 '23 at 12:24

1 Answers1

5

Expanding on my comments, there's nothing special with a recursive function call. It's just like any other function call.

Lets take the following example:

int c(int k)
{
    return 0;
}

int b(int k)
{
    return k + c(k - 1);
}

int a(int k)
{
    return k + b(k - 1);
}

int main(void)
{
    std::cout << a(2) << '\n';
}

The call a(2) will do return 2 + b(1);
The call b(1) will do return 1 + c(0);
The call c(0) will do return 0;

This is really the same as the call sum(2) from the recursive example in the question:

The call sum(2) will do return 2 + sum(1);
The call sum(1) will do return 1 + sum(0);
The call sum(0) will do return 0;

The result is 2 + 1 + 0 which equals 3.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621