0

Anyone can explain me what means void f(int& r) and why the output of this code is 1?

why the output of the code following code is 1? #include <iostream> using namespace std; void f(int& r){ } int main() { int i = 3; f(i); cout << f; } // output=1 thanks in advance

18th Dec 2016, 1:03 PM
Telmo Pinto
Telmo Pinto - avatar
6 Answers
+ 1
& means that the parameter is the address of a Int. This means that if you change the value of "r" inside the function, you're changing the value of i, outside the function, because they are pointing at the same address. If you use f(int r) you're just passing the value of i, then, r will have the same value of i, but now if you change r inside the function, it will not change i, outside. //The code is incomplete, no?
18th Dec 2016, 1:16 PM
Sheemin
Sheemin - avatar
0
Это неправильный код
18th Dec 2016, 1:06 PM
Чистобаев Андрей
Чистобаев Андрей - avatar
0
why is it wrong?
18th Dec 2016, 1:15 PM
Telmo Pinto
Telmo Pinto - avatar
0
What does it mean: cout<<f;? f is function so you must use () to call it. And what do you want to print? f returns void!
18th Dec 2016, 2:01 PM
Чистобаев Андрей
Чистобаев Андрей - avatar
0
yes i know that. i just dont understand why the output is 1...
18th Dec 2016, 2:06 PM
Telmo Pinto
Telmo Pinto - avatar
0
The real question is how do you print a function that returns nothing :/ You simply can't. The code should not even compile
18th Dec 2016, 2:12 PM
Sheemin
Sheemin - avatar