Question about recursion?

closed account (3UMLy60M)
Can somebody please explain to me what the output would be if I were to call:

m3(5);

and also can you explain why? I'm new to C++ and this is confusing me.

Thanks.

1
2
3
4
5
6
7
int m3(int n)
{
	if (n==1)
		return 25;
	else
		return n + m3(n-1);
}
Last edited on
Why don't you run the code or step thru it in a debugger?
Excatly what kbw said. Run the code. But you don't really need to run the code to see what this particular program will return for n = 5

will be:
5 + 4 + 3 + 2 + 25
closed account (3UMLy60M)
After posting the question I did run it through a debugger and the output was 39. However, I would like to understand why is was 39.

Edit: Never mind, both of these answers were able to solve my problem. Thanks everyone for your help.
Last edited on
Topic archived. No new replies allowed.