-
Consider the following C function :
void swap (int a, int b)
{ int temp; temp = a;
a = b;
b = temp;
}
In order to exchange the values of two variables x and y.
-
- Call swap (x, y)
- Call swap (& x, & y)
- Swap (x, y) cannot be used as it does not return any value
- Swap (x, y) cannot be used as the parameters are passed by value
- Call swap (x, y)
Correct Option: D
Why a, b and c are incorrect? a) call swap (x, y) will not cause any effect on x and y as parameters are passed by value. b) call swap (&x, &y) will no work as function swap() expects values not addresses (or pointers). c) swap (x, y) cannot be used but reason given is not correct. Here the function takes the arguments by value.
Option (a) sends parameter by value but only the local variable a & b will be exchanged but not the actual variables x & y so incorrect.
Option (b) is incorrect sending address of x & y.
Option (c) swap (x, y) is usable there is no need to return.
Option (d) is the opposite statement of option (a), it says that the values are passed by value so won't swap so the option is correct.