Help me in Understanding Recursion 2

Hello everyone, I need help in understanding the recursion on how does it end in 12345678910. When I tried to understand this problem, I expected that the output should be 10987654321. How does the problem end up in 12345678910? I'm having difficulties in learning the logical flow of recursion.
#include <iostream>

using namespace std;

void recursion (int number) {
    if (number == 0)
    return;
    recursion (number-1);
    cout << number;
}

int main()
{
   recursion(10);

    return 0;
}
Last edited on
Already asked and answered at http://www.cplusplus.com/forum/general/280842/
Yeah, but I just need more information on it because I'm still having difficulties in dealing with it. I'm having a hard time in understanding it mentally.
Last edited on
If you want to count UP to n, then
- count up to n-1, then write n

If you want to count DOWN from n, then
- write n, then count down from n-1

When you feel the urge to write 0 ... then you do nothing further.
@ForgottenLaw - then add a new post to the existing topic asking for more info, don't just start a new one on the same issue.
Adding extra output may help explain what's going on.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

void recursion (int number) {
    cout << "Entering recursion(" << number  << ")\n";
    if (number == 0)
    return;
    recursion (number-1);
    cout << number;
    cout << "\nLeaving recursion(" << number  << ")\n";
    
}

int main()
{
   recursion(10);

    return 0;
}

Entering recursion(10)
Entering recursion(9)
Entering recursion(8)
Entering recursion(7)
Entering recursion(6)
Entering recursion(5)
Entering recursion(4)
Entering recursion(3)
Entering recursion(2)
Entering recursion(1)
Entering recursion(0)
1
Leaving recursion(1)
2
Leaving recursion(2)
3
Leaving recursion(3)
4
Leaving recursion(4)
5
Leaving recursion(5)
6
Leaving recursion(6)
7
Leaving recursion(7)
8
Leaving recursion(8)
9
Leaving recursion(9)
10
Leaving recursion(10)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

void recursion(int num)
{
   if (0 == num)
   {
      return;
   }

   std::cout << "Calling recursion(" << num - 1 << ")\n";
   recursion(num - 1);

   std::cout << num << '\n';
}

int main()
{
   recursion(10);
}

Calling recursion(9)
Calling recursion(8)
Calling recursion(7)
Calling recursion(6)
Calling recursion(5)
Calling recursion(4)
Calling recursion(3)
Calling recursion(2)
Calling recursion(1)
Calling recursion(0)
1
2
3
4
5
6
7
8
9
10

How functions work behind the scenes using the "call stack":
https://www.learncpp.com/cpp-tutorial/the-stack-and-the-heap/

How recursion works:
https://www.learncpp.com/cpp-tutorial/recursion/

This is why you were told to do the output BEFORE the function call in your previous thread:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

void recursion(int num)
{
   if (0 == num)
   {
      return;
   }

   std::cout << num << '\n';

   std::cout << "Calling recursion(" << num - 1 << ")\n";
   recursion(num - 1);
}

int main()
{
   recursion(10);
}

10
Calling recursion(9)
9
Calling recursion(8)
8
Calling recursion(7)
7
Calling recursion(6)
6
Calling recursion(5)
5
Calling recursion(4)
4
Calling recursion(3)
3
Calling recursion(2)
2
Calling recursion(1)
1
Calling recursion(0)
Thank you so much for all the information you gave me. Now I know how does it work. It is because of the call stack which is not yet discussed to us.
Last edited on
yes, recursion gives you a 'free' stack data structure effectively.
the call stack is absolutely critical to debugging large programs, and your IDE/debugger will provide it to you on demand when debugging. Ironically its hard to follow during recursions, but its mighty helpful when you end up in function foo () with a nullptr or nan or other unexpected value and want to know "where did THAT come from".
Topic archived. No new replies allowed.