Skip to content
BENIBIX Learn

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.

Development🧩 Programming Concepts

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.

Example
A score of 5 is passed to a function that adds 1. Inside it is 6, but back outside the score is still 5.
Why it matters
It tells you whether a function can change your data. Guess wrong and a bug hides in a value you thought was safe.
Full entry
Development🧩 Programming Concepts

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.

Example
A score of 5 is passed by reference to a function that adds 1. Back outside, the score now reads 6.
Why it matters
It lets a function update big data without copying it. It is also a common source of surprise changes.
Full entry

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.