mystery function

how does this mystery function work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 int mystery(int a, int b)
{
	if(b==1)
		return a; 
	else 
		return a +mystery (a,b-1);
}
int main()
{
int x,y;
cout<<"Enter two integers:\n";
cin>>x;
cin>>y;
cout<<"the result is "<<mystery(x,y)<<endl;
getch();
}

closed account (Dy7SLyTq)
Its going to return whatever the value of a is. Then its going to keep taking one from b until it ='s one. So if b < 1 then ur going to get infinite recursion
looks like it returns a*b for any value where b>=1
It's the same as this:

1
2
3
4
int mystery(int a, int b)
{
    return a * b;
}

It multiplies a times b.

Edit: well, to be more precise, it's more like this:

1
2
3
4
int mystery(int a, int b)
{
    return b < 1 ? throw 1 : a * b;
}
Last edited on
closed account (Dy7SLyTq)
Ah sorry my mistake... I see that now... its doing it brainf*** style
what if line 6 was...
return a +mystery (a,b>1 ? b-1:b+1);

would that not make b eventually equal 1?
yes it multiples two integers but how does it work for example does it if b =3 it minuses b-1 and multiples with 'a' could someone explain that in detail how does that happen becuase if b>1 then return a+ mystery(a,b-1);// how does this recursive call call work
Recursion can be difficult to visualize. I find it's easiest to explain by plugging in numbers.

Let's say you call:

mystery(5,1). Since b==1, this means that mystery will simply return a (as indicated on line 4). Therefore... mystery(5,1) == 5

mystery(5,2) returns 5+mystery(5,1) as indicated on line 6.
Since we've already determined mystery(5,1) == 5, this means that:

mystery(5,2) == 5+mystery(5,1) == 5+5 == 10

and so on... and so on...

1
2
3
4
5
mystery(5,1) == 5
mystery(5,2) == 5+mystery(5,1) == 5+5 == 10
mystery(5,3) == 5+mystery(5,2) == 5+10 == 15
mystery(5,4) == 5+mystery(5,3) == 5+15 == 20
mystery(5,5) == 5+mystery(5,4) == 5+20 == 25
Awesome i couldn't find better explanation than this than anywhere just one tiny thing if b==1 it will return a value so does recursion automatically having storing capabilities like doesnt it lose its determined value 25 and return the intial int a value of 5
When you call a function, the function body executes. When that function returns, the return value is fed to the code that called it. IE:

1
2
3
4
5
6
7
8
9
10
11
int foo()
{
    int butt = 3;
    butt += 2;
    return butt;
}

int main()
{
    cout << foo();
}


Here, when main calls 'foo', foo's body will execute... all it's code will run... and it will return a value (in this example it returns 5).

That value is fed back to the code that called it (here, it gets fed back to main, since main called foo). The function call evaluates to whatever the function returned... so since we are sending the foo() call to cout... we're actually sending the value foo returns to cout. Since foo() returns 5, this will send 5 to cout... which will print 5 to the user.



Recursive calls are no different. A called function gets executed and the returned value gets fed back to whoever calls it. The only change is that the function is calling itself.

So when we call mystery(5,3) from main... the flow works like this:

1) main calls mystery(5,3)
2) mystery(5,3) calls mystery(5,2)
3) mystery(5,2) calls mystery(5,1)
4) mystery(5,1) doesn't call anything, but instead just returns 5
5) execution returns to mystery(5,2), which takes the returned value (5), adds 5 to it, and returns it (returning 10)
6) execution returns to mystery(5,3), which takes the returned value (10), adds 5 to it and returns (returning 15)
7) execution returns to main, with the value of 15 having been returned.


mystery does not "remember" previous calls. Each time you call it, its logic is rerun and its value is recalculated.
at point 4 when mystery returns 5 why will it return execution return again to mystery becuase as u said urself in point 3 mystery is (5,1) and again execution starts again from the value (5,2) and then it adds +1 to b in your point 6 and makes (5,3) and it returns calll back to the main function when b is not even of value 1 now i am in honest confusion
return doesn't [necessarily] return to main. It returns to whoever called the function.

Since mystery is calling mystery... when the 2nd mystery returns, it returns to the 1st mystery... since that's who called it.

Here is a diagram I whipped up in Paint:

http://oi44.tinypic.com/2ekltuc.jpg
Last edited on
Trust in Humanity restored :D Now i know how it works thank you so much for giving it so much time it would have had harder to understand without the diagram
Topic archived. No new replies allowed.