+ 5

How this code work?

how this code work? i break my brain Can someone explain this? https://code.sololearn.com/cKeMAOacrloS/?ref=app

26th Mar 2018, 1:25 PM
Edil Sarsekeev
Edil Sarsekeev - avatar
6 Réponses
+ 4
it work on the concept of call by reference... function f() is having arguments which defines reference by & this sign...... now.... f(a, a); means &a = a; and &b = a; when you make change to &a and &b it will change to a and a...... now a = 2 and b = 4 that means now a is 4 and b is also 4 return a+b means 4+4 as I told making change to &a and &b will change value of a..... now cout<<a<<b<<c; a is 4 b is 7 // because we changed only a not b c is containing a+b so it will be 8 hope you got it....
26th Mar 2018, 1:33 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 18
cool, that is a great challenge and 2 good explanations, also thank you very much from my side 👍😉
26th Mar 2018, 1:45 PM
tooselfish
tooselfish - avatar
+ 8
int x(int &a, int &b){ a=3; b=4; return a+b; } int main() { int a=2; int b=7; int c = x(a, a); cout << a<< b << c; return 0; } The value of a is 2. Function x receives two parameters by reference, which means that both parameters will refer to the same local variables in main. Variable a is passed into the function through both parameters. In the function, variable a is altered to 3, and then to 4. (Remember that both the function parameters now refer to the same variable) The function the returns 4+4, which is 8, assigned to variable c. Program prints 478
26th Mar 2018, 1:28 PM
Hatsy Rei
Hatsy Rei - avatar
+ 5
Thanks guys!
26th Mar 2018, 1:35 PM
Edil Sarsekeev
Edil Sarsekeev - avatar
+ 2
thanks for marking best
26th Mar 2018, 1:40 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
@tooselfish welcome
26th Mar 2018, 1:49 PM
Raj Chhatrala
Raj Chhatrala - avatar