Introduction
If you're new to coding, you have probably heard of call by value and call by reference in c++ programming language. But what are they exactly? In this blog post, we're going to take a look at the basics of both concepts in C++ so that even novice coders can better understand them. We'll also be discussing when it's best to use one or the other while writing code in C++, making sure that your program runs efficiently and correctly. So whether you've been wanting to learn more about these principles or simply brush up on your knowledge, fear not—we've got all the details right here!
Ways of passing the data to the function
There are two ways to pass the data to the function in the c++ language, are
- Call by value in c++ language
- Call by reference in c++ language
What is call by value in C++
Call by value in C++ is a programming method that passes the value of a variable to a function as an argument, instead of passing the memory address of the variable. In call by value, the values of arguments are copied into local variables, and any changes made to that value while within the scope of the function do not affect in original values stored within the calling program. This ensures that programs remain stable by preventing any unexpected changes to data that could have a wide-reaching impact on an application. Call by value is widely used as it allows for more robust and bug-free programming processes, saving developers time and money.
Example
#include <iostream>
using class std;
void change(int data);
int main()
{
int data = 4;
change(data);
cout << "The value of the data is: " << data<< endl;
return 0;
}
void change(int data)
{
data = 5;
}
Output
The value of the data is: 4
What is call by reference in C++
Call by reference in C++ is a powerful method of passing arguments to functions. It allows objects that are passed as arguments to reference their original counterpart in the callers' scope, allowing manipulation of arguments at both call time and return time. This method allows for a unique set of operations that call by value cannot, such as fixing values to be used between many interrelated calls, or even returning multiple values from one call. With its ability to give many advantages over a call by value, call by reference in C++ stands out as being very advantageous when programming certain algorithms.
Example
#include<iostream>
using class std;
void swap(int *a, int *b)
{
int swap;
swap=*a;
*a=*b;
*b=swap;
}
int main()
{
int a=4500, b=1300;
swap(&a, &b); // passing value to function
cout<<"The value of a is: "<<a<<endl;
cout<<"The value of b is: "<<b<<endl;
return 0;
}
Output
The value of a is: 1300
The value of b is: 4500
Difference between call by value and call by reference in C++
There are some differences between call by value and call by reference, those are
Call by value | Call by reference |
In call by value, a copy of the value is passed to the function | In call by reference, an address is passed to the function |
If any changes that are happened inside the function do not reflect on other functions | If any changes that are happened inside the function do reflect outside the function as well |
Formal and actual arguments will be generated in various memory locations | Formal and actual arguments will be generated in the same memory location |
Summary
If you are a C++ programmer, it is important to understand the difference between call by value and call by reference. Call by value means that the value of the argument is copied into the parameter. Call by reference means that a reference to the argument is passed into the parameter. This article contains the difference between the call by value and call by reference. The main difference between these two methods is that with the call by reference, changes made to the parameter will be reflected in the argument, while this is not the case with the call by value. Be sure to keep this distinction in mind when programming in C++!
Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.