-3

Can you please explain to me the differences between two functions:

function &a(){
    return something;
}

and

function b(){
    return something;
}

Thanks!

duyvu1311
  • 93
  • 2
  • 11

2 Answers2

1

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.

Aerospace
  • 1,210
  • 11
  • 18
0

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.
Gautam3164
  • 27,319
  • 9
  • 56
  • 81