2

So my doubt is, I was trying out call by value, While running the given code, Swapping happens when I write the function definition after int main() But if I cut and paste the function definition above int main(), the swap does not take place. Why is that?


#include<iostream>
#include<string>
#include<vector>
#include<bitset>
#include<fstream>
using namespace std;
#define ADDU 1
#define SUBU 3
#define AND 4
#define OR  5
#define NOR 7
#define MemSize 65536
void swap(int a, int b)
{
    int temp = a;
    a = b;
    b = temp;
}

int main(){
    // int a = 20;
    // int *p = &a;
    // cout<<"P: "<<p<<endl<<"*P gives: "<<*p<<endl<<"&p gives: "<<&p<<endl<<"&a : "<<&a;;

    int x,y;
    x = 10;
    y = 20;
    cout<<"Before Swapping: "<<"x: "<<x<<endl<<"y: "<<y<<endl;
    swap(x,y);
    cout<<"After Swapping: "<<"x: "<<x<<endl<<"y: "<<y<<endl;
}
  • 5
    Can't be certain without seeing your includes, and/or whether you have `using namespace std;` in your code, but my guess is, that after you move it after the `main`: `std::swap` is being used, instead of your function. Your function doesn't do the swapping, because values are passed by value, instead of by reference, so it only swaps the copies of those variables. – Algirdas Preidžius Oct 09 '19 at 23:14
  • I've added all the includes now –  Oct 09 '19 at 23:20
  • to fix your own swap, just change signature to `void swap(int& a, int& b)` (as @AlgirdasPreidžius said pass by reference) – fas Oct 09 '19 at 23:21
  • Yes, I'm aware of that. I just wanted to know the reason why swap happened here. Also, can you elaborate on what "swaps the copies of those variables" mean? @a –  Oct 09 '19 at 23:23
  • 2
    @MarshallMathew Due to `using namespace std;`, the `std::swap` is being used (even if you didn't include the header, which defines it: it might have been included by other STL headers), so relevant reading: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), and possible(?) duplicate: [C++ passing by reference or by value?](https://stackoverflow.com/questions/35679436/c-passing-by-reference-or-by-value) – Algirdas Preidžius Oct 09 '19 at 23:24
  • @MarshallMathew `x` and `y` are copied when you pass them into your `swap` (as `a` and `b`), and you swap `a` and `b`, not `x` and `y` – fas Oct 09 '19 at 23:25
  • 1
    Folks, we have an answer section for answers. Thanks. – Lightness Races in Orbit Oct 09 '19 at 23:26
  • @MarshallMathew "_on what "swaps the copies of those variables" mean?_" Function takes a copy of variables. Swaps those. Returns. Since swapping was done on the copies, no change is observed outside the function. – Algirdas Preidžius Oct 09 '19 at 23:26
  • 1
    @LightnessRacesinOrbit "_Folks, we have an answer section for answers. Thanks._" 1) Yes, but my first "answer" was just a guess, before the question was edited to include the missing information. Answers should be answers, not guesses. 2) Even after the edit, I think that this question already has answers somewhere else (which I linked to). – Algirdas Preidžius Oct 09 '19 at 23:29
  • Moving that `swap` so it comes after `main` is a bit of a red herring. Deleting it would have the same effect. – Pete Becker Oct 10 '19 at 02:40
  • 1
    @AlgirdasPreidžius If all you have is a guess, it's best not to post anything. But deffo don't do it in the comments where it cannot be peer reviewed. When you already suspect your suggestion is wrong, that's double-bad. You can simply leave the answering to those who do know and move on to another question that you feel you can answer. :) The comments section is not for providing solutions, confidently or otherwise. Cheers. – Lightness Races in Orbit Oct 10 '19 at 09:39
  • @LightnessRacesinOrbit "_If all you have is a guess, it's best not to post anything._" Even if it's a guess: it's, most likely, an educated guess. Typically, it is formed, by taking into account similar problems, seen in the past, but without necessary information, in current question, to confirm those. Hence, if you take note of those preconceptions, before stating your guess at a problem: I don't see anything wrong with doing so, instead of just writing dry, blanket statement of "Please provide [mre]." – Algirdas Preidžius Oct 10 '19 at 14:16
  • @LightnessRacesinOrbit "_When you already suspect your suggestion is wrong, that's double-bad._" Why do you think, that I suspect that my suggestion is wrong? I didn't. I suspected, that my suggestion is right, but the question lacked sufficient information, to confirm it. But, still, in essence, it was just a guess, even if I was 99% sure, that it was the issue observed. And even after the question was edited to include entire information: I feel it's a question compounded of 3 other questions, already answered on SO, so it's better to close it as a duplicate, of those, in my opinion. – Algirdas Preidžius Oct 10 '19 at 14:18

1 Answers1

9

Your swap function doesn't really swap anything, because it takes its arguments by value rather than by reference. All you're doing is manipulating variables that are local to that function.

When you don't introduce it until after main, it's not in scope when you call it, so std::swap is used instead. std::swap works correctly.

Although you didn't specifically say std::swap, you wrote using namespace std; which removes that requirement (good reason not to do it!!). And, although you did not #include <algorithm>, you cannot guarantee which standard headers may end up including other ones by virtue of how the implementation is constructed.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055