why this output 5 and why not 2? #include <iostream> using namespace std; void func(int &x) { x=5; } int main() { int x=2; func(x); cout<<x; } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why this output 5 and why not 2? #include <iostream> using namespace std; void func(int &x) { x=5; } int main() { int x=2; func(x); cout<<x; }

24th Aug 2016, 3:18 AM
Lekhraj Singh
Lekhraj Singh - avatar
7 Answers
+ 3
because you call the function func() and his reference as argument is the variable x inside the function that have value 5...when we call the function...the function pick the reference variale x inside of that function...not the variable on main() function...hope that this helps ;-)
24th Aug 2016, 6:13 AM
Carlos Augusto Silva Gurgel
Carlos Augusto Silva Gurgel - avatar
+ 1
Not at all...hey have you whatsapp...if you have lets have some programming chat at there...would be interesting and I will be honored
24th Aug 2016, 6:20 AM
Carlos Augusto Silva Gurgel
Carlos Augusto Silva Gurgel - avatar
0
thank Carlos
24th Aug 2016, 6:16 AM
Lekhraj Singh
Lekhraj Singh - avatar
0
yea, what's your WhatsApp no.
24th Aug 2016, 6:25 AM
Lekhraj Singh
Lekhraj Singh - avatar
0
its from Brazil. .. here: +5597981080851
24th Aug 2016, 7:04 AM
Carlos Augusto Silva Gurgel
Carlos Augusto Silva Gurgel - avatar
0
and your no.?
24th Aug 2016, 7:06 AM
Carlos Augusto Silva Gurgel
Carlos Augusto Silva Gurgel - avatar
0
+918107350482
24th Aug 2016, 7:09 AM
Lekhraj Singh
Lekhraj Singh - avatar