#include <stdio.h> int confusion (int x, int y) { x = 2*x + y; return x; } void main() { int x = 2; int y = 5; y = confusion(y,x); x = confusion(4,x); printf("x is %d and y is %d\n", x, y); }Question: What is printed?
Answer: x is 10 and y is 12
The reason this program is so confusing is that the same variable names are used. It's easy to get mixed up. Here's the exact same program with the names changed: (remember that you can choose whatever names you want.)
#include <stdio.h> int double_first_plus_second (int first, int second) { first = 2*first + second; return first; } void main() { int x = 2; int y = 5; y = double_first_plus_second(y,x); x = double_first_plus_second(4,x); printf("x is %d and y is %d\n", x, y); }Did you get it right this time?
The first time the function is called from main, y is assigned the value 2*5 + 2, which is 12.
The second time the function is called, x is assigned the value 2*4 + 2, which is 10.
These are the values that are printed. Notice that the first function call didn't affect the value of x.