Multiple choice

Find out the output of the given program or error if any.

int main()
{
 void swap(int,int);
int a=10, b=20;
swap(a,b); //Line 6
 printf(%d,%d,a,b);
 getch();
}
 void swap( int x, int y) //Line 9
{
 int temp=x; x=y; y=temp;
}

  1. 10, 20

  2. 10, 10

  3. 20, 10

  4. 20, 20

  5. Error in the program

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

In above program, function is called by value. In call by value parameter's copies are made. Initially at Line 6 from where swap function is called,  a = 10, b=20.    a --> | 10 |   b-->| 20 |           Now, control goes to Line 9 and values of a,b are copied to x,y. Now, x=10, y=20 and a = 10, b= 20  a --> | 10 |   b-->| 20 |  x --> | 10 |   y-->| 20 |   After these values of x and y are swapped, x=20, y=10 but still values of a and b are 10 and 20.     a --> | 10 |   b-->| 20 |  x --> | 20 |   y-->| 10 |   So whatever changes we have done in swap function is on x and y, which is a separate copy of a and b.  So this is the correct answer.