Discussing some coding with someone recently, it came up how function parameters could be passed in the C++ language. Looking around on the Net to give him a good summary, couldn't quite find one, so thought writing up one myself. So what passing by value, reference and pointer means, what they do, what to look out for when using them.

The three ways of passing parameters to a function in C++

  • void MyFunction(MyClass myObject); // Passing by Value
  • void MyFunction(MyClass &myObject); // Passing by Reference
  • void MyFunction(MyClass* myObject); // Passing by Pointer

In the first two cases (by Value, by Reference) when calling the function, the object is passed to it as-is, such as:
MyFunction(someObjectOfMyClass);
When passing by pointer, the address of an object needs to be passed, such as:
MyFunction(&someObjectOfMyClass);

In the first two cases (by Value, by Reference) within the function, the object's methods are accessed with '.', such as:
myObject.DoSomething();
If the parameter was passed by Pointer, within the function the object's methods are accessed with '->' (dereference), such as:
myObject->DoSomething();

How passing by Value works (C and C++)

When passing by Value, a copy of the entire parameter is passed to the function (usually) using the stack. C and C++ work identically in this regard. Even passing objects is similar: you can pass a structure by value in C which would be passed by a copy placed on the stack, this works similarly in C++ for objects.

The difference is that C++ classes can have Copy constructors, which Copy constructor, given that the object is copied, gets called.

How passing by Reference works (C++ only)

When passing by Reference, in fact the address to the parameter is passed to the function. In terms of low-level behaviour, this is pretty much identical to how passing by Pointer works.

The reason for the existence of passing by Reference is that it allows for writing code less prone to programming errors by the compiler ensuring that:

  • Only valid parameters are passed in this manner (you can't normally pass NULL or possibly a pointer to garbage).
  • Within the function, only the object can be accessed (you can't use the reference like a pointer to start doing pointer arithmetic on it).

How passing by Pointer works (C and C++)

When passing by Pointer, the address to the parameter is passed to the function.

The caller can provide any address, notably including NULL.

The function may use pointer arithmetic or indexing to reach other data than the parameter itself, a common use-case for this is passing arrays in this manner to a function.

Good practice: Use const

With either passing by Reference or passing by Pointer, it is a good practice to use const whenever possible. On the function's declaration, this indicates clearly that the function only reads the parameter, making it easier to deduct how the function works. Within the function, it avoids programming or design errors (the latter if someone, or in these times, something - looking at AI agents) starts changing the parameter's value (or the object's state).

  • void MyFunction(const MyClass &myObject); // Passing by const Reference
  • void MyFunction(const MyClass* myObject); // Passing by Pointer to const

When using pointers, it is worth noting that the parameter itself can also be const, though in a function declaration some compilers or static analysis tools might warn on this. In a function definition such prevents the modification of the pointer in the function body (such as to make it point to something else), however in the declaration (usually in a header) such const keywords have no effect.

  • void MyFunction(const MyClass* const myObject); // Passing by const Pointer to const
  • void MyFunction(MyClass* const myObject); // Passing by const Pointer.

In the latter, note that the pointed object remains mutable!

Check for NULL when passing by Pointer

That is, in the function receiving the parameter passed by Pointer.

Especially in C++ one of the reasons for using passing by Pointer might be to make the parameter optional by allowing NULL to be passed in it.

Otherwise if passing by Pointer is used extensively in the codebase (such as C++ written by more C-minded people), it is prudent to look out for this issue. NULL could easily creep into pointers, default value remaining due to an edge case in initialisation, someone misunderstanding it to be allowed when doing some quick patch in some code calling the function to implement a feature, such things.

When to use which one?

Passing by Value is a good choice for base (fundamental) types and for anything small and not having a copy constructor if there is no need for writing the parameter. What may be considered "small" depends somewhat on the target and the compiler. Twice the pointer size of the target might be a good guide for maximum size (if the compiler supports passing structures and objects in registers at all).

In C++, passing by const Reference for anything too large to be passed by Value is a good general rule to follow, only considering other ways (non-const Reference, Pointer) if the design really needs it.

Further it may depend on style. Passing by non-const Reference has the issue that in the program flow it may be confusing. Such parameters in function calls look the same like parameters passed by Value (or const Reference), so it becomes less clear when an object may change state by the call for someone not too familiar with the code. However passing by pointer has less constraints (more opportunities for bugs), so some teams might still prefer using pass by Reference even in such cases.

Comments

No comments so far. Be the first to comment!

Make a comment

Rules

  1. Please be polite (Leave all your trolls in their respective caves).
  2. If #1 fails, don't feed 'em. They bite.
  3. No links allowed. It won't pass. Neither chains. Use '(dot)' notation.
  4. Spam reeks.
  5. Your mail address is only visible to me: I understand you also don't like #4.
  6. The mail address you provide is also used to fetch your Gravatar.
  7. For the privacy-conscious: It only really needs to look like an address.
  8. Danger! High voltage! Right between your "Post Comment" button and ground.
  9. Still want to comment? Go ahead! :)