Const Reference vs Normal Parameter Passing in C++

In C++, when we call a function we can also pass the parameters to the function in many ways like pass by value, pass by reference, pass by pointer, and by const reference. Each parameter-passing technique has its own advantages and disadvantages. In this article, we will learn the difference between normal parameter passing and passing by const reference in C++.

Normal Parameter Passing in C++

In C++, normal parameter passing is also known as “pass-by-value“, in this parameter passing technique the compiler creates a copy of the argument being passed. Any changes made to formal parameters within a function are not reflected in the original arguments. If the parameters passed are very large, then the copying process becomes very tedious and results in wasting our storage and CPU cycles.

Syntax for Normal Parameter Passing

returnType functionName(dataType parameterName);  //pass by value

Example

The below example demonstrates how we can pass parameters to function by value in C++.

C++
// C++ program to demonstrate normal parameter passing

#include <iostream>
using namespace std;

// function to modify the value
void increase(int num) { num += 5; }

int main()
{
    // define avariable
    int x = 10;
    // calling increase function by passing parameter x
    increase(x);
    // printing the variable x
    cout << x; // 10
}

Output
10

Explanation: In the above example, the value of actual parameter x remains 10 even after the increase() function call because num is a copy of x so changes are not reflected back in x.

Const Reference in C++

In the “pass by const reference” technique, a const reference (also known as an alias) to the original argument is passed. This means that we cannot modify the actual parameters within the function. By using a reference, we avoid making a copy of the argument, so we can also pass large objects or parameters to functions.

Syntax for Const Reference Parameter Passing

returnType functionName(const dataType& paramName);

Example

The below example demonstrates how we can pass by const reference in C++.

C++
// C++ program to demonstrate pass by const reference

#include <iostream>
using namespace std;

// function to modify the value
void increase(const int& num)
{
    num += 5; // Error: read-only variable is not assignable
}

int main()
{

    // define avariable
    int x = 10;
    // calling increase function by passing parameter x
    increase(x);
    // printing the variable x
    cout << x;
    return 0;
}


Output

error: assignment of read-only reference ‘num’

Explanation: In the above example, compiler throws a compile-time error because here we are trying to modify a read-only reference which is not possible.

Difference Between Const Reference and Normal Parameter Passing

The below table demonstrates the key differences between const reference and normal parameter passing:

Feature

Const Reference


Normal Parameter Passing

Definition

In this argument are passed as a reference to the original value, but does not allow the function to modify that value.

In this argument are passed to a function by copying the actual value into the function’s formal parameter.

Syntax

void func(const int& x);

void func(int x);

Memory Efficiency

More efficient with large data types as it avoids copying the data. Only the reference is passed.

Less efficient with large data types due to the overhead of copying data.


Modification of Data


The original data cannot be modified within the function, ensuring data integrity.


Modifications to the data inside the function do not affect the original data, as only a copy is modified.

Use Case

It is suitable for passing large or complex data types when we want to ensure that the original data should not be modified.

It is suitable only for small data types or when we want the function to work with a copy of the data so that the original data remains unchanged.

Flexibility

Cannot be used if the function needs to modify the passed data.

Provides complete freedom to modify the copied data inside the function without any risk to the original data.

Conclusion

In conclusion, the choice between const reference and normal parameter passing in C++ depends on the specific requirements of our program. If we’re working with large objects and want to avoid the overhead of copying, const reference can be a good choice. However, if we want the function to modify the arguments within oour function, normal parameter passing will be preferred.