+ 1
I don't understand how to comes following output???could anyone explain? #include <iostream> using namespace std; 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<<endl<<b<<endl<<c; } output:- 4 7 8
2 Réponses
+ 3
the function x uses two variables passed by reference so it is not the actual value but a memory address. In main you pass the same variable for both arguments so both a and b point to the same memory location.
int x(int &a,int &b)
{
a=3;
b=4; /* because a and b point to the same location
here you indirectly make this change a=4*/
return a+b;
}
sorry for bad english
0
thank bogdan👌👍