+ 2

C++, STUCK (swap using function) why 66 not 63

Can anyone explain me the logic of this program? Bool exchange (int &X,int y){ Int temp = X; X=y; Y=temp; Return true; } Int main(){ Int a=3, b=6; If(exchange(a,b)) Cout<<b<<a; Else Cout<<10; Return 0; }

26th Jun 2021, 2:32 AM
Nitin Bisht
Nitin Bisht - avatar
12 Respuestas
+ 3
if you want to 'exchange' values of arguments, the mistake was to NOT put & before second argument... (so the output will be 36... not 63 nor 66)
26th Jun 2021, 2:51 AM
visph
visph - avatar
+ 2
`exchange` function takes argument <X> by reference, meaning changes to <X> in `exchange` will reflect to the original variable <a> (defined in main function). Argument <y> is taken by value, meaning changes to <y> in `exchange` will not reflect to the original variable <b> (defined in main). * Here's what happens in `exchange` 1. A local variable <temp> defined using value of argument <X>. 2. Value of argument <X> (3) is changed to value of argument <y> (6). Remember changes to <X> will reflect to the original variable <a> in main function because <X> was passed by reference. 3. Value of argument <y> is changed to value of local variable <temp>. Changes to <y> will not reflect to original variable <b> in main because <y> was passed by value. 4. Function `exchange` completes its work, returning boolean true for the caller. * Back in main function after call to `exchange`. Value of <a> had changed to 6, but value of <b> remains. Because <a> was passed by reference, but <b> was passed by value.
26th Jun 2021, 2:59 AM
Ipang
+ 1
The future is now thanks to science[LESS ACTIVE] my professor saying to me I did mistake by putting & in front of X , remove it and the program will work fine lol . I need professor like you (THE FUTURE ) who can clear my doubts thank you so much .
26th Jun 2021, 2:49 AM
Nitin Bisht
Nitin Bisht - avatar
+ 1
Ipang now I understand the whole logic behind this program thanks for giving time to answer my question I really appreciate it thank you so much .
26th Jun 2021, 3:03 AM
Nitin Bisht
Nitin Bisht - avatar
+ 1
Thanks, @Martin Taylor. I thought it passes by address. Now I understood it . Actually, x is passed by reference. Now your explanation clears everything👍.
26th Jun 2021, 11:59 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
Martin Taylor passing by reference technically pass the address of the variable isn't it? that's only for programmer that it makes a difference between using * or & in arguments: with * the argument has to be considered as a pointer, while with & the argument has to be considered as a variable... or I'm wrong? ... so & simplify the works with variables, but * is more explicit, isn't it? but in both cases, under the hood, that's the address of variable(s) wich is passed as argument (pushed on the call stack), rather than the value when using the naked variable name, if I'm not wrong ^^
26th Jun 2021, 5:35 PM
visph
visph - avatar
+ 1
Martin Taylor I have not said I'm not agree with you: I am... I just pointed the fact that the code was probably using a custom exchange function with an obscure purpose in mind (insane as were -- most of? -- challenge codes) ;)
26th Jun 2021, 7:00 PM
visph
visph - avatar
0
My pleasure bro 👌
26th Jun 2021, 3:06 AM
Ipang
0
Martin Taylor also, in OP's code, returning true isn't pointless, as if exchange return value is not boolean the if statement would not be possible, even if it's pointless as the return value is always true: that sounds more as a (modified?) challenge question than a real life code ^^
26th Jun 2021, 5:42 PM
visph
visph - avatar
0
Martin Taylor that's why I said that OP's exchange function isn't pointless, even if the conditional statement is pointless and why I guess that the code sounds like a (maybe modified) challenge question rather than a real life code ^^
26th Jun 2021, 6:25 PM
visph
visph - avatar
0
Martin Taylor by "modified" I was meaning "not necessarly volontary", such as forgotting the second &... and as far as I know, challenges have plenty of pointless codes, just to confuse players (not real life codes) ;P
26th Jun 2021, 6:34 PM
visph
visph - avatar
- 3
https://code.sololearn.com/ca104A1a10A1 #include<iostream> using namespace std; bool exchange (int &X,int &y){ int temp = X; X=y; y=temp; return true; } int main(){ int a=3, b=6; if(exchange(a,b)) cout<<a<<b; else cout<<10; return 0; }
27th Jun 2021, 2:52 AM
Abhishek Kumar
Abhishek Kumar - avatar