Before coming to us, you should have done something like this to see where each number is actually coming from:
Code:
#include <iostream>
using namespace std;
int global = 2;
int rek(int par)
{
cout << "par passed into rek: " << par << endl;
global = global + par;
if (par < 7)
return rek(par * 2) + 2;
else
return 0;
}
int main()
{
cout << "value returned by rek: " << rek(1) << endl;
cout << "global: " << global << endl;
return 0;
}
Which has output:
Code:
par passed into rek: 1
par passed into rek: 2
par passed into rek: 4
par passed into rek: 8
value returned by rek: 6
global: 17
So that 6 is the value returned by rek. We can work this out by hand. We know
rek(1) = rek(2) + 2
rek(2) = rek(4) + 2
rek(4) = rek(8) + 2
rek(8) = 0
So substituting back down:
rek(4) = rek(8) + 2 = 0 + 2 = 2
rek(2) = rek(4) + 2 = 2 + 2 = 4
rek(1) = rek(2) + 2 = 4 + 2 = 6