+ 21
Explain this question please? Wht happened in this program explain please .
6 Respuestas
+ 5
Because function x is called with same variable in position of both arguments, inside function variables a and b share same memory location. You can see that by adding test to function x:
int x(int &a, int &b)
{
if(&a==&b) cout<<"Same address"<<endl;
a=3;
b=4;
return a+b;
}
So first (a=3) both get value 3 and then (b=4) both get value 4.
+ 4
You are passing a (a,a) to function x by reference (int &a, int&b), meaning you can change their values in function x.
You might be confused because of
(int&a, int &b),
but you can change the names of these to
(int&a, int&y)
You are still changing ONLY a because you are passing ONLY a in main
int c = x(a,a)
And in function x(int&a, int&b)
a=3; -------> a = 3
b=4; ---------> a=4; REMEMBER int&b is
a name holding a reference
to a.
+ 3
This question has been asked before and read comments.
checkout
https://www.sololearn.com/Discuss/2307927/?ref=app
+ 1
comments are pretty great:))