&-Operator in function argument list? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
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);

27th Nov 2016, 12:13 PM
Cameron Reuschel
Cameron Reuschel - avatar
2 ответов
+ 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.
27th Nov 2016, 9:41 PM
Mohammed Maaz
Mohammed Maaz - avatar
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.
27th Nov 2016, 12:36 PM
Drauchris
Drauchris - avatar