Why Reference( & ) ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why Reference( & ) ?

I saw friend in c++, in the note section it written as," Note you must pass reference(&) to the .....", so why to pass reference ???😢😢 Here's the link ---- https://www.sololearn.com/learn/CPlusPlus/1899/

6th Jun 2018, 1:59 PM
Vipin Kumar Pandey
3 Answers
+ 3
Adding to that performance gain, references allow you to modify the variable also outside the function.
6th Jun 2018, 3:28 PM
Timon Paßlick
+ 1
a reference is kind of like a pointer, normaly if you pass something to a function, it gets copied into the variable/parameter that the function uses, but if you use a reference the object doesn‘t get copied but instead only the reference to the object, this can save time, since objects are pretty big and more importantly in this case every modification you make to the object(for example changing a member) will be visible from outside the function, which it wouldn‘t be if the object was copied into the function.
6th Jun 2018, 2:34 PM
Max
Max - avatar
+ 1
Without passing reference, the member variables have to be be all copied. It wastes memory and takes much time.
6th Jun 2018, 2:36 PM
Disvolviĝo;
Disvolviĝo; - avatar