Consider the following C function void swap ( int x, int y ) { int tmp; tmp =…

2017

Consider the following C function

void swap ( int x, int y )
{
int tmp;
tmp = x;
x= y;
y = tmp;
}

In order to exchange the values of two variables a and b:

  1. A.

    Call swap (a, b)

  2. B.

    Call swap (&a, &b)

  3. C.

    swap(a, b) cannot be used as it does not return any value

  4. D.

    swap(a, b) cannot be used as the parameters passed by value

Attempted by 601 students.

Show answer & explanation

Correct answer: D

In C, function arguments are passed by value. The swap function receives copies of the variables a and b. Modifying x and y inside the function does not affect the original variables outside. To exchange values, pointers must be used as parameters.

Explore the full course: Isro