recursion question

hi guys I just wrote a snippet of code that uses recursion but I don't actually understand what it does the main thing I don't understand is why it prints hello 10 times after it prints all the numbers,I thought it would just print all the numbers than just print hello once,so how come it prints hello ten times and how come it prints hello after one another instead of after a number?

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

using namespace std;

void adam(int x){

    if(x>=1){

        cout << "number : " << x << endl;
        adam(x-1);
    }

    cout << "hello" << endl;

}


int main()
{
   adam(10);

}
closed account (E0p9LyTq)
You are calling recursively the function 10 times, so hello gets printed 10 times.

Why does it print the 10 numbers before printing the 10 hellos? The way the function's execution state recursively is pushed onto and unwound from the stack. Look at where you are calling the function within the function. Before you print hello.
Line 9: You print the first number (10).
Line 10: You call adam() again with 9. You haven't gotten to line 13 yet.
Line 9-10 repeat 9 more times pushing a total of 10 instances of adam() on the stack.
Now the 10th instance of adam() calls adam one more time, but now x-1 is zero, so control passes to line 13 and hello is printed. The 10th instance of adam exits. Where does control return to? Line 11 in the 9th instance of adam. Control passes to line 13, hello is printed again. The 9th instance of adam exits. Control returns to line 11 in the 8th instance of adam. This continues until the first instance of adam exits and control returns to main.

i.e. every instance of adam on the stack is going to flow to line 13. Since you have 10 instances of adam on the stack, hello is going to appear 10 times.

Because hello is after you return from adam, hello is going to be printed "on the way out" of adam as each instance is popped from the stack.

thanks guys that clears things up a lot but what if I used numbers in the example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <iostream>

using namespace std;

void printNum(int num){

   cout << num;

  if(num< 9){

        printNum(num+1);
    }

    cout << num<< endl;

}


int main()
{
   printNum(1);

}



correct me if I'm wrong but would this do as follows

prints out num which is one then calls printNum again with 2,prints out 2 then calls printNum steps repeated until it hits 9

then it prints out 9,printNum(9) gets pushed off the stack then it returns to the caller printNum(8) but this is the part that's rather confusing the number is now 8,8 is less than 9 how come the function does not get called yet again with 8 in it?
Last edited on
closed account (E0p9LyTq)
Here's a nice diagram showing how recursion works:

http://www.c-sharpcorner.com/UploadFile/6897bc/recursive-functions/Images/rec3.JPG
Thanks furry I'll check it out =)
Topic archived. No new replies allowed.