Pass by Value vs Pass by Reference
With pass by value, a function gets a copy, so your own value stays the same. With pass by reference, it gets a link to your value, so its changes show outside too.
Pass by Value
The function gets a copy. Changing the copy leaves the first one alone.
Pass by value means a function gets a copy of each argument. If the function changes its copy, the caller's value stays the same. C, Java and many other languages pass simple values like numbers this way. It keeps functions from changing things by surprise. The cost is copying, which can be slow for very big data.
Pass by Reference
The function gets a link to the real value. So its changes show outside too.
Pass by reference means a function gets a link to the caller's own variable, not a copy. If the function changes it, the caller sees the change. C++ does this when a parameter is marked with &. It avoids copying big data. But a function can now change things the caller did not expect.
In one line
- Pass by Value
- The function gets a copy. Changing the copy leaves the first one alone.
- Pass by Reference
- The function gets a link to the real value. So its changes show outside too.