Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
void main()
{
int x=5,y=10;
swap2(x,y);
printf("%d %dn",x,y)
swap2(x,y);
printf("%d %dn",x,y);
}

int swap2(int a,int b)
{
int temp;
temp=a;
b=a;
a=temp;
return 0;
}
Posted
Updated 29-Sep-15 22:31pm
v3
Comments
Krunal Rohit 30-Sep-15 3:29am    
Where's Swap function ?

-KR

It all depends on swap2 function definition. If, for instance, the swap2 function is defined for destroying the Universe then the program would abruptly terminate.


The following C program
C
#include <stdio.h>
int main()
{
  int x = 5, y = 10;
  swap2(x, y);
  printf("%d, %d\n", x, y);
  swap2(x, y);
  printf("%d, %d\n", x, y);
  return 0;
}


Will always output
5, 10
5, 10


for any correct implementation of the swap2 function, since the swap2 prototype must be
void swap(int x, int b);

(actually you can use another type for return value).


In C++ programming language the scenario may change, defining, for instance, swap2 this way
C++
void swap2( int & a, int & b)
{
  int t = a;
  a = b;
  b = t;
}


Could you guess the actual output?
 
Share this answer
 
v3
Run it, and find out.
We can't - we don't have your swap2 function, so it won't even compile for us, much less give us an output!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900