Skip to main content

Functions

Call by Value, Call by Pointer, and Call by Reference

Understanding how function parameters are passed in C++ is fundamental to mastering the language. Before diving into the types of parameter passing, it's essential to understand what it means to pass a parameter to a function.

What is Parameter Passing?

When a function is called in C++, you often pass data to it. This data is known as a parameter or argument. The way this data is transferred from the calling function to the called function is referred to as parameter passing.

Goals of Parameter Passing

  • Allow functions to use input data.
  • Enable modification of data (when needed).
  • Avoid unnecessary data duplication.

The mechanism used to pass the parameter determines whether:

  • The function gets a copy or a reference to the original.
  • Changes in the function reflect in the original data.
  • The performance is optimal or not.

C++ offers three main methods to achieve this, which we will now explore in detail.

Call by Value

What is it?
Call by Value means that a copy of the actual value is passed to the function. Any changes made to the parameter inside the function do not affect the original variable.

Characteristics:

  • The function receives a copy.
  • Original data remains unmodified.
  • Memory is allocated for the new variable copy.

Code Example:

Output:

Use Cases:

  • Safe when the function should not alter the original value.
  • Efficient for primitive types.

Drawbacks:

  • Not efficient for large objects.
  • Changes are local to the function.

Additional Insight:

  • Best practice is to avoid passing large structs or classes by value as copying incurs performance penalties.
  • Used often when implementing mathematical functions or algorithms with no side effects.

Call by Pointer

What is it?
Call by Pointer involves passing the address of the variable. The function can access and modify the actual data by dereferencing the pointer.

Characteristics:

  • Function can modify the original variable.
  • Useful for dynamic memory.
  • Requires pointer syntax.

Code Example:

Output:

Use Cases:

  • When function needs to modify multiple values.
  • Used in dynamic memory allocation (e.g., new, delete).
  • Necessary in C-style APIs and low-level hardware interfacing.

Drawbacks:

  • Pointer syntax can lead to bugs if mishandled.
  • Risk of segmentation faults.
  • Pointers can become dangling or uninitialized.

Additional Insight:

  • Defensive coding practices include checking for nullptr before dereferencing.
  • Passing arrays to functions internally translates to passing pointers.

Call by Reference

What is it?

Call by Reference passes the alias of the actual variable. The function works directly on the original variable without making a copy.

Characteristics:

  • Uses & symbol in function parameters.
  • No need to use pointers explicitly.
  • Original value is modified.

Code Example:

Output:

Use Cases:

  • Cleaner syntax than pointers.
  • Preferred for operator overloading.
  • Efficient for large objects or classes.

Drawbacks:

  • Can cause side effects if not used carefully.
  • Not always obvious to a reader that value will change.

Additional Insight:

  • Commonly used in function chaining and fluent interfaces.
  • Enables overloading operators like +, =, <<, etc.
  • Should be used with const when mutation is not desired.

Comparison Table

Feature Description Call by Value Call by Pointer Call by Reference
What is passed Copy of variable Address of variable Alias to original variable
Modifies original variable No Yes Yes
Use of & No Yes (in function call) Yes (in function header)
Use of * No Yes No
STL usage Rare Rare Common (const T&)
Multiple return value support No Yes (via multiple pointers) Yes (via multiple refs)