What does the following code print out? Can someone explain?
int Confusion(int x, int y)
{
x = 2 * x + y;
return x;
}
int Main()
{
int x = 2, y = 5;
y = Confusion(y, x);
x = Confusion(y, x);
Console.WriteLine(x, y);
return 0;
}
Ok.
The method Confusion takes two parameters, x and y. It returns the first, x, after doubling it and adding the value off the second paramter, y, on top. You could also write return 2 * x + y straight away.
So, in Main you have two variables, x and y with the values 2 and 5.
In the first step you set the value of y to two times y plus x. So y stores now the value 12.
In the second line you modify x. You take y two times (12 * 2 = 24) and add 2 (x) on top. X has now the value 26.
The console will print out: 26, 12.
Hope i was able to help.
2 Likes
Yes very much so
I see that it is very important how you are calling the Confusion method as x and y are reversed so the values in the Confusion method are also reversed. That was something I wasn’t sure was right
Thank you.