When to pass by value and pass by reference require some theoretical understanding which includes the cost of parameter initialization and accessing the parameter.

Cost of passing arguments by value and reference

When an argument is passed by value, its value is copied into the parameter. If the object is small and easy to copy without any extra setup, it is inexpensive. However, there are objects which may be large and require other extra setup (some class does opening files, setting up dynamic memory etc.) and copying them would be expensive.

On the other side, a reference is just passed where no copy is required.

Accessing parameter when passed by value and reference

When a function call is made, for the optimization, compiler can place reference to the object or copy of the object (if size is small) into CPU register than the RAM. Accessing from CPU register is faster as compared to RAM.

Accessing the parameter when passed by value only require one time access request to CPU register or RAM to get the value. But in the case of pass by reference, there is an extra step of an access request to RAM. First is for the reference (to CPU register) and second one for object being referenced (to RAM).

Conclusion

  1. If the cost of copying object is similar to binding, passing by value would be optimal as there is not extra step in accessing the parameter.
  2. If the cost of copying is expensive, it is better to pass by reference cost of copying is dominating over accessing the value.

References

  1. https://www.learncpp.com/cpp-tutorial/pass-by-const-lvalue-reference/#:~:text=The%20cost%20of%20pass%20by%20value%20vs%20pass%20by%20reference