execution flow

hi, i recently came about this piece of code and i am confused as to why it gives the output that it does

here is the code:

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
27
28
#include <iostream>
#include <string>
using namespace std;
 
 
string mysteryRecursive(int counter)
{
     string result = "#";
 
     if (counter == 0)
     {
          return result;
     }
     else
     {
          cout << "Counter is " << counter << endl;
          result = "*" + mysteryRecursive(--counter);
          cout << "Result is " << result << endl;
          return result;
     }
}
 
 
int main( )
{
   cout << mysteryRecursive(5);
   return 0;
}


the output of this code is:

Counter is 5
Counter is 4
Counter is 3
Counter is 2
Counter is 1
Result is *#
Result is **#
Result is ***#
Result is ****#
Result is *****#
*****#

i am a beginner so please excuse my noobie questions

1) what causes the function to loop and cout more than two statements
ie) why was the output not:
Counter is 5
Result is *#

2) why is the output not in chronological order
ie) why was the output not:
Counter is 5
Result is *#
Counter is 4
Result is **#.....ect

thank you for your time =)
It's a bit hard to explain without drawing a picture. If you step through the code line by line you should be able to work it out.
Note that mysteryRecursive calls itself on line 17.
1) The answer is recursion of course. This means that function string mysteryRecursive(int counter) calls itself. Of course there should be a way to stop the recursion (as in other case there would be an infinite recursion loop).

That condition is fulfilled by the argument of the function: counter. See in line 17 that counter is first decreased then used as argument (because of the prefix decrement operator --counter.

When enter inf function mysteryRecursive the argument is examined to 0 (line 10): if (counter == 0). If this condition is true line 12 return result; is executed and the function never goes beyond this line.
The first encounter of return terminates the function.

So that is why string is increased. I hope that answers your first question.

2) Now that I hope you got the picture of recursion examine how it is invoked:
-line 20 is executed first cout << mysteryRecursive(5); where mysteryRecursive() is invoked with argument 5.
-Inside this function now (that is recursive) a recursive loop starts. Since argument is different than 0 the execution continues up to line 17 where a new invocation of the function is found:
result = "*" + mysteryRecursive(--counter);
Consider that up to now we haven't reached line 18 cout << "Result is " << result << endl; but we have reached line 16 cout << "Counter is " << counter << endl;. This will continue until condition in line 10 is fulfilled. Then it will return the string created.

That's why all
Counter is
appear before all
Result is
.

Hope that solved some questions. Be cautious though recursion is powerful but rather subtle point of the language.


Topic archived. No new replies allowed.