Pass by reference/return value - advantages and disadvantages

Explain the difference between pass by reference and using return values with respect to both how these features work and the advantages and disadvantages of each. which technique is most consistent with the principle of low coupling and why?

Return value is used when the function needs to return only a value/variable.
advantages its easy to see where the variable comes from. disadvantages, A new variable need to be create.

Pass by reference is used when the function needs to return more than a value/variable. However if the value needs to be changed the variable needs to have a & on front of it. Also it is seen as a pointer in the function.
Advantages A new variable don't need to be create.
disadvantages, it can passed to any function therefore it could be low coupling.

This is a final exam question not a homework. is the answer good enough?

Thanks for looking!!
Last edited on
If it's an exam, you should answer what you were taught, not what other software engineers do in practice.

(in practice, return a value when the function's job is to create a new object, such as string::substr or operator+, pass by (non-const) reference when the function's job is to modify an existing object, such as std::swap)
Return value is used when the function needs to return only a value. If for example there is a maths operation in the function and only one value is going back to the other function, it can be return by value. Or just one value to come back from the function.
Advantages it’s easy to see that the value is returned from the function.
Disadvantages, A new variable need to be create. A return value can only be a simple type like an integer or float, not an array or a struct

When a function needs to change more than one variable from another function, it needs to be pass be reference and it seen as a pointer. However if we need to change the value of this variable in that function, a & needs to be on the front of the variable name.
Advantages A new variable don't need to be create, an array or struct can be pass.
Disadvantages, You need to be careful that you only change the parameters that are necessary, otherwise you may change a passed in variable unintentionally.
PBR since, Low coupling between modules means that each module should have limited ability to interact with data belonging to other modules.
Topic archived. No new replies allowed.