0

The cout statement in function call swap is not executing. Why?

#include <iostream>

using namespace std;

void swap(int *a, int *b) {
  int temp = *a;
  *a = *b;
  *b = temp;
  cout << "Value Swapped" << endl;
}

int main() {
  int x = 400, y = 100;
  swap(x, y);
  cout << "x = " << x << " y = " << y << endl;
  return 0;
}

I have expected below output:

Value Swapped
x = 100 y = 400

But the output I have got is:

x = 100 y = 400
cHao
  • 84,970
  • 20
  • 145
  • 172
  • I have expected below output: Value Swapped x = 100 y = 400 But the output I have got is: x = 100 y = 400 – Trishal Mandrik Sep 25 '19 at 16:44
  • 3
    1) Did you try stepping through your code with a debugger, to confirm, that your function is called instead of `std::swap`? 2) Related: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) 3) "_I have expected below output ..._" Please provide such information in the question, not in the comments. – Algirdas Preidžius Sep 25 '19 at 16:46
  • your `swap` function treats arguments as addresses while you are passing integers. that's not the only problem though – mangusta Sep 25 '19 at 16:46
  • You are actually calling `std::swap`. Don't use `using namespace`... – BiagioF Sep 25 '19 at 16:46
  • General debugging note: When you get an inexplicable mystery bug, `using namespace std` should be one of the first things to go. – user4581301 Sep 25 '19 at 17:05
  • `s/When you get an inexplicable mystery bug, //` :) Seriously, if you hate typing `std::cout` that much, just say `using std::cout;`. – cHao Sep 25 '19 at 17:07

4 Answers4

3

You're not seeing "Value swapped" because your swap function never gets called. Instead, you're calling std::swap<int>.

If you hadn't said using namespace std;, you'd have found out that your swap function is being passed integers, but was expecting pointers. Reason #1 not to use using namespace std; -- you can easily end up not calling what you think you are. :)

cHao
  • 84,970
  • 20
  • 145
  • 172
  • *'passed integers, but was expecting pointers'* – that results in a compilation error already... – Aconcagua Sep 26 '19 at 08:27
  • @Aconcagua it does, unless there's another overload that matches, which is the case once OP spilled `::std` into `::` ;) – Quentin Sep 26 '19 at 08:55
  • @Quentin *'If you hadn't said'* – so in *this* context, no other overload any more... – Aconcagua Sep 26 '19 at 08:57
  • 2
    @Aconcagua I think the three of us are agreeing here. Remove `using namespace std;`, call the expected function, get a compile-time error because the argument types don't match, find the mistake. – Quentin Sep 26 '19 at 08:58
0

your function swap is expecting pointers as arguments, but you are passing integers. So instead of calling your function, it's calling the c++ swap function. To better understand this, change the function name 'swap' to any other name like 'swapp' and you'll see this error:

invalid conversion from int to int*

write swap(&x, &y) and you'll get your desired output

Sudip Sarker
  • 421
  • 3
  • 7
0

Consider choosing a unique enough identifier name such that there will not be a conflict with a namespace identifier (as in std::swap). If you can't choose the name of your identifier, put it inside of a namespace.

Secondly, if the compiler does not enforce the function declaration that you state, you should manually check to see that you passed the variables correctly (i.e., &x and &y instead of x and y).

C. R. Ward
  • 93
  • 5
0

using namespace std imports another equally named function from namespace std into global namespace, so you end up in having two overloads:

swap(int&, int&); // from std; actually a template instantiation
swap(int*, int*);

You call swap as

swap(x, y);

Now consider: Which are the types of x and y, are they pointers? So decide yourself, which overload will get called?

If you hadn't imported the second overload via using namespace std, which generally is considered bad practice anyway (well, you got a victim of the pitfalls of right away...), you would have ended up in a compilation error instead (integers aren't converted to pointers implicitly!).

To get your function called, you need to make pointers from:

swap(&x, &y);
//   ^   ^

Actually, that would have worked even with the imported namespace (despite of being bad practice), as the imported overload is a template, yours an ordinary function, thus yours is considered the more specialised overload and thus will be preferred.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59