The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+14 votes
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

asked in Compiler Design by Veteran (59.4k points)
edited by | 2.9k views
+1

5 Answers

+22 votes
Best answer

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

answered by Boss (14.8k points)
edited by
+3

This Might Give Clear Understanding

+8 votes
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..
answered by Boss (23.4k points)
0
Yes option D should be correct.
0 votes

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

answered by Loyal (8.1k points)
–1 vote
ans d)
answered by Active (5.2k points)
–2 votes

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

answered by Loyal (7k points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

35,506 questions
42,827 answers
121,678 comments
42,181 users