+ 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

24th Aug 2016, 6:20 AM
Lekhraj Singh
Lekhraj Singh - avatar
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
24th Aug 2016, 7:45 AM
bogdan
bogdan - avatar
0
thank bogdan👌👍
24th Aug 2016, 7:50 AM
Lekhraj Singh
Lekhraj Singh - avatar