Call By Value And Call By Reference In C++
C++, a powerful programming language, offers a wide range of features and functionalities. Passing arguments to functions is one of the most fundamental ideas in C++.
A function must be "called" to carry out the requested action. In C++, we can either call a function by value or by reference.
Calling by value and calling by reference have a slight distinction. The kinds of parameters that are passed to each of them vary.
When you call by value, a copy of the real value is passed on. When a call by reference is made, a reference (address) specifying the location of the value's storage is given.
Call by Value in C++:
“Pass by copy and calls by value are other terms for a call by value.” Passing in the copy of the actual parameter that is stored in memory as the formal parameters is the definition of pass-by-value.
When we pass a value to a function using the pass-by-value method, the following take place:
- The actual parameters are entered.
- The same is duplicated in memory.
- The formal parameter is passed as this copy.
Because we pass a copy, the parameters are kept in separate parts of memory. As a result, any modification made inside the function will not impact the starting values.
Implementing the Call-by-value:
//c++ program to perform pass-by-value
#include <iostream>
using namespace std;
//Writing a function to swap the two numbers
void swap(int,int);
int main()
{
int a,b;
cout<<"\n Enter First Number : ";
cin>>a;
cout<<"\n Enter Second Number : ";
cin>>b;
cout<<"\n Before Swapping the Value : \n"<<" "<<a<<"\t"<<b<<"\n";
swap(a,b);
cout<<"\nOutside Function After Swapping the Value : \n"<<" "<<a<<"\t"<<b<<"\n";
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"Inside Function After Swapping the Value : \n"<<" "<<a<<"\t"<<b;
}
OUTPUT:
Working:
The main purpose of the and b variable values swapping function is to do just that. We observed no change in the values, though.
This is because the dummy variables (in this case, a and b), which are kept in a different memory address, are where all the changes are made.
Advantages of Call-by-value:
- Functions can alter the given values without changing the underlying data.
- Excellent for use with multithreaded or asynchronous programming when we need to keep track of values.
- When converting a string to an integer, the pass-by-value method can be used to preserve the value of the string.
Disadvantages of Call-by-value:
- Passing a copy of the data while using a pass-by-value increases the size of the data. This would be fine with smaller programs, but it might impact cost-effectiveness in bigger programs.
- Copies of values can take a long time to make if the argument is too big.
- The modified values cannot be transmitted through the parameters.
Call by Reference in C++:
“Pass by address or call by reference is other terms for call by reference.” Instead of giving the values of variables when invoking a function, we pass a reference to the values.
The following happens when we use pass-by-reference to pass a reference to a function:
- The real parameters are passed in.
- The function receives a reference variable that has been generated for it.
- Any variable may be referred to as a reference variable.
Both variables now point to the identical value because we pass a duplicate address. Therefore, any modification to the function would result in an update to the real values.
Implementing the call-by-Reference:
//c++ program to perform pass-by-reference
#include <iostream>
using namespace std;
//Writing a function to swap the two numbers
void swap(int* a, int* b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
cout<<"Inside Function After Swapping the Value : \n"<<" "<<*a<<"\t"<<*b; }
//Implementing the main function
int main() {
int x,y;
cout<<"\n Enter First Number : ";
cin>>x;
cout<<"\n Enter Second Number : ";
cin>>y;
cout<<"\n Before Swapping the Value : \n"<<" "<<x<<"\t"<<y<<"\n";
//pass by reference
swap(&x, &y);
cout<<"\nOutside Function After Swapping the Value : \n"<<" "<<x<<"\t"<<y<<"\n";
return 0;
}
OUTPUT:
Working:
- The formal and real parameters in the C++ mentioned above program point to the same memory address. X and an are, therefore, both aliases for the same memory address. In the same way, y and b.
- Therefore, all the modifications done by the swap() method are visible in the starting values.
- The x and y addresses are contained in the variables a and b, respectively.
- We are utilizing the dereferencing variable * in the function definition to access the value at the specified memory location. This indicates that we can use *a and *b, respectively, to access the data kept at x (5) and y (15). Simply put, *a = 5 and *b = 15.
Advantages of Call-by-Reference:
- Since no copies of the data are made, we can create effective programs.
- Through the use of parameters, it improves how simple information may be transferred between functions.
Disadvantages of Call-by-Reference:
- This approach is not advised when creating multithreaded software because it may result in data loss.
- Pass-by-reference can only be used within a single program because it is a high-level method.