Pass by value vs Pass by Reference (Basic)

Published by on September 7, 2019
Categories: C++ Programming

When passing parameters to a function there are two basic primary ways to do so. One is called Pass by Value and the other is Pass by Reference.

When passing by value, we copy the value to the function and make it available to the function. As this is a copy it can be mutated, without affecting the value passed to the function. This is fine for primitive types (int, bool, etc), but can be quite a memory issue on larger types (int64, big structs). I prefer to keep my pass by value use to types of 8 bytes or less.

Example of Pass by Value:

Output result:

Value before Pass by Value: 100!
SomeFunction: 200
Value after Pass by Value: 100!

As you can see we modified the Value in SomeFunction, but the original passed in Val was not modified by the function.

 

Pass by Reference

Pass by Reference creates a reference to the type, its 4 bytes and is basically an address to that type. Benefit of using Pass by Reference is to reduce memory usage for larger types (for example SomeStruct is 24 bytes, passing this by value is copying the struct thus using another 24 bytes, if we passed this by reference we would only use 4 bytes) and to allow the passed in type to be mutated if we so wish. Below is a example of a mutatable Pass by Reference:

Output Result

Value before Pass by Reference: 100!
SomeFunction: 200
Value after Pass by Reference: 200!

As you can see from the above, the original Val was 100, when we called SomeFunction, we added 100 to it, but as this was Passed by Reference, we also mutated the original passed in Val. Meaning after the call to SomeFunction, our Val is now 200.

What if you don’t want the passed in type to be mutated but still want to use Pass by Reference to save on copying? Use const!

In the below example we use const to promise the calling code we won’t modify the passed in reference.

The above code won’t compile but will instead throw an error. The error maybe different based on the compiler in use, but the general error will be:

PassByReferenceConst.cpp: In function ‘void SomeFunction(const int&)’:
PassByReferenceConst.cpp:7:14: error: assignment of read-only reference ‘Value’
Value += 100;

This basically means we tried to modify Value but Value was Read Only (caused by the const). This basically as the error says, means Value is Read Only. If we simply remove the assignment, the code will compile fine:

Output would be:

Value before Pass by Reference: 100!
SomeFunction: 100
Value after Pass by Reference: 100!

Summary:

Use Pass by Value for primitive types (int, bool, unsigned int) which should not mutate the passed in type. Use Pass by Reference to allow mutation of the passed in type, use Pass by Reference (const) to provide a read-only reference to the passed in type. Generally any type over 8 bytes can/should be passed by reference if possible.

Like i said, this is basic and simple and does not cover everything. But should hopefully help newcomers to the idea of Pass by Value/Pass by Reference

Leave a Reply

Your email address will not be published. Required fields are marked *