These are (some) of the ways to define a function in C++ (depending on what the function should do):
type f_name (type par) {/*...*/}
type f_name(type& par) {/*...*/}
where the first one passes as argument a copy of par
to the function, whereas the second one passes a reference to the variable par
, which means that if something 'happens' to the variable, it will be reflected in the variable itself.
My confusion is with the following definition
type& f_name(type& par) {/*...*/}
I don't understand what the &
means in the return type of the function.
Also when should I and shouldn't I use it?
EDIT: I would appreciate if the answer came with a representative example