0
&-Operator in function argument list?
Can anybody explain what happens when you call a function with an & in its argument list? Let's say function(int &x) doubles the value of x and you call it via y=2; function(y);
2 Answers
+ 2
Every time you pass a variable to a function, it's copy is passed in actual and the original variable is preserved.
However when a function has & in it's parameter then this means that the value you pass to it while calling will be passed as a reference. So if the argument's value is modified in the function then it will also get modified in actual or in the whole program.
0
the value of y will be manipulated because you give in the reference. without passing the reference you would just give the function a copy of y and not y itself.