Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Pass by Value vs Pass by Reference in C++

Pass by Value vs Pass by Reference in C++

How data is passed to functions and why it matters for performance and behavior

Ihor Gudzyk

by Ihor Gudzyk

C++ Developer

Mar, 2026
8 min read

facebooklinkedintwitter
copy
Pass by Value vs Pass by Reference in C++

When working with functions in C++, one important decision is how you pass data to them. At first glance, it may seem like all function arguments behave the same, but under the hood, there is a crucial difference.

C++ allows you to pass data either as a copy or as a reference to the original variable. This affects not only performance, but also whether a function can modify the input.

Understanding this distinction helps you write code that is more efficient, predictable, and easier to maintain, especially when working with larger data structures or real-world applications.

Pass by Value

When you pass a variable by value, a copy of that variable is created and sent to the function. The function works with its own independent copy, not the original data.

void update(int x) {
    x = 10;
}

int a = 5;
update(a);
  • a remains unchanged;

  • only the copy inside the function is modified.

Pass by value provides isolation, meaning the original variable is protected from any changes made inside the function. It works well with small data types like int, float, and char, where copying is inexpensive, and is especially useful in situations where you do not want to modify the original value, helping you write safer code with fewer side effects.

Pass by Reference

When you pass a variable by reference, the function receives a reference to the original variable, not a copy. This means any changes made inside the function will directly affect the original data.

void update(int& x) {
    x = 10;
}

int a = 5;
update(a);
  • a is now 10;

  • the original variable is modified directly.

Pass by reference provides direct access, meaning the function works with the actual variable rather than a copy. It is especially useful for large data types like std::vector, std::string, or structs, where copying would be expensive, as well as in situations where you need to modify the original value or optimize performance by avoiding unnecessary copies.

While pass by reference is powerful and efficient, it requires careful use, since any changes inside the function directly affect the original data and can introduce unintended side effects.

Run Code from Your Browser - No Installation Required

Run Code from Your Browser - No Installation Required

When to Use Pass by Value

Pass by value is a good choice when you want to keep your data safe from modification and the cost of copying is minimal. It works best with small, simple types where creating a copy is fast and does not impact performance.

Using pass by value also makes your functions easier to reason about, since they cannot accidentally change the original data.

SituationWhy Use Value
Small data types (int, double)Copying is fast
Read-only operationsPrevents modification
Safer function behaviorNo side effects
Independent processingWorks on its own copy

When to Use Pass by Reference

Pass by reference is ideal when you want to avoid unnecessary copying and work directly with the original data. It is especially useful for large objects, where copying would be expensive, or when a function needs to modify the input.

However, since it allows direct modification, it should be used carefully to avoid unintended side effects.

SituationWhy Use Reference
Large objects (vector, string)Avoids expensive copies
Need to modify dataChanges original variable
Performance-critical codeMore efficient
Shared data accessWorks with same object

Const Reference

Sometimes you want the efficiency of references without allowing modification. That is where const reference is useful.

By using const, you tell the compiler that the function can read the data but not change it. This avoids copying while keeping your code safe.

void print(const std::vector<int>& vec) {
    for (int x : vec)
        std::cout << x << " ";
}

A const reference is commonly used when working with large objects that should not be modified. It helps reduce unnecessary memory usage while making the function’s intent clear. You can use it:

  • when working with large read-only data;
  • when you want to prevent accidental modification;
  • when performance matters but safety is required;
  • when passing containers or complex structures.

Using const references is considered a best practice in modern C++, as it provides a balance between efficiency and safety, making your code both faster and more predictable.

Start Learning Coding today and boost your Career Potential

Start Learning Coding today and boost your Career Potential

Common Mistakes

One common mistake is passing large objects by value without realizing the performance cost. This creates unnecessary copies, which can slow down your program, especially when working with containers like vectors or strings. In such cases, using a reference or const reference is usually a better choice.

Another issue is overusing references without proper care. Since references allow direct modification of the original data, it can lead to unintended side effects and harder-to-debug code. Forgetting to use const when modification is not needed is also a frequent problem, as it removes an important layer of safety.

FAQs

Q: What is the main difference between pass by value and pass by reference?

A: Pass by value creates a copy of the data, while pass by reference works with the original variable.

Q: Is pass by reference always better for performance?

A: Not always. For small data types, pass by value is often just as fast and simpler.

Q: When should I use const reference?

A: When you want to avoid copying large data but also prevent the function from modifying it.

Q: Can pass by reference cause bugs?

A: Yes, if used carelessly, it can lead to unintended changes in the original data.

Q: Do references replace pointers in modern C++?

A: Not completely, but they are often preferred for safer and cleaner code when possible.

Oliko art. hyödyllinen?

Jaa:

facebooklinkedintwitter
copy

Oliko art. hyödyllinen?

Jaa:

facebooklinkedintwitter
copy

Tämän artikkelin sisältö

Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt