2.9k views

Consider the following 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$.

1. call $swap(x, y)$

2. call $swap(\&x, \&y)$

3. $swap (x, y)$ cannot be used as it does not return any value

4. $swap (x, y)$ cannot be used as the parameters are passed by value

edited | 2.9k views
+1

ans (D).

option A will not swap the values because it is passed by value...

option B will not swap the value

  void swap(int a, int b) in which arguments will not take address because its value not pointer

option C is false , given reason is wrong

and option D is correct .. we cant use $swap(x,y)$ because it is pass value function call which will not swap the values

edited by
+3

This Might Give Clear Understanding

I think, option D will be answer.

Because in the given function, parameters are used as passed by value.So there will be no change in actual parameters which is defined under the main function..
0
Yes option D should be correct.

Pass by value: In this approach, we pass the copy of actual variables in function as a parameter. Hence any modification on parameters inside the function will not reflect in the actual variable.

Therefore to exchange the values of x and y, we cannot use pass by value( given code is for pass by value). So option D is correct

–1 vote
ans d)

Ans: D swap (x, y) cannot be used as the parameters are passed by value