Can you please explain to me the differences between two functions:
function &a(){
return something;
}
and
function b(){
return something;
}
Thanks!
The first returns a reference to something
, the second a copy of something
.
In first case, when the caller modify the returned value, something
will be modified as a global variable do.
In the second case, modifying a copy as no effect to the source.
An ampersand before a function name means the function will return a reference to a variable instead of the value.
According to this LINK
Returning by reference is useful when you want to use a function to find to which
variable a reference should be bound. Do not use return-by-reference to increase
performance. The engine will automatically optimize this on its own. Only return
references when you have a valid technical reason to do so.