question?

hey guys i have a question why
the output of this program is
3 , 4 if
3 , 4 else
4 , 3 if
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
	recursion(2,3,4);
	
}
void recursion(int count, int y, int x)
{
	if(count==1)
	{
		cout<<y<<" "<<x<<"if"<<endl;
	}
	else 
	{
		recursion(count-1,y, x);
		cout<<y<<" "<<x<<"else"<<endl;
		recursion(count-1,x,y); '
   /*i change the x and y's placement because i want to experiment
    about recursion can you explain why it interchange values
*/	
}
}
Last edited on
They reverse because you reversed them:

In line 16 you send x to parameter y, y to parameter x for the next recursion.

The variables y and x are private to each recursion of the function, so if you repeat line 16 over and over, the values will switch each time.
Topic archived. No new replies allowed.