446,194 Members | 884 Online Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

# query regarding swap function

 P: 6 1> #include using namespace std; Expand|Select|Wrap|Line Numbers void swap(int& i, int& j)  {     int tmp = i;     i = j;     j = tmp; cout<<"The value after swap of x and y is"<>x>>y; swap(x,y); cout<<"The value after swap of x and y is"< Expand|Select|Wrap|Line Numbers #include  using namespace std; main()     {         int a,b;         //void swap(int a,int b);         cout<<"enter the value of a and b"<> a>>b;         swap (a,b);         cout<<"The value after swap is"<
3 Replies

 P: 51 1> #include using namespace std; void swap(int& i, int& j) { int tmp = i; i = j; j = tmp; cout<<"The value after swap of x and y is"<>x>>y; swap(x,y); cout<<"The value after swap of x and y is"< #include using namespace std; main() { int a,b; //void swap(int a,int b); cout<<"enter the value of a and b"<> a>>b; swap (a,b); cout<<"The value after swap is"< main() { int a,b; void swap(int a,int b); cout<<"enter the value of a and b"<> a>>b; swap (a,b); cout<<"The value after swap is"<

 Expert 100+ P: 315 1> #include using namespace std; void swap(int& i, int& j) { int tmp = i; i = j; j = tmp; cout<<"The value after swap of x and y is"<>x>>y; swap(x,y); cout<<"The value after swap of x and y is"< #include using namespace std; main() { int a,b; //void swap(int a,int b); cout<<"enter the value of a and b"<> a>>b; swap (a,b); cout<<"The value after swap is"<

 P: 6 The reason is that your 2nd program uses the swap() function of the STL, since your function is not in scope at the time of the call. Try to completely comment out your swap function and things will still be swapped :) The first version works, since you define your function swap() before the call. So it's in scope. thanks a lot Mar 19 '07 #4 