Call By Address is also known as Call By Pointers. In this method, the programmer provides the addresses of the actual arguments to the formal parameters. Subsequently, the function uses these addresses to work with the actual arguments in the program. Essentially, the function parameters are passed as addresses when employing the Call by Address/Pass by Address approach. The calling function sends the parameter addresses. The function definition involves the use of pointer variables. Through the Call by Address mechanism, the function is capable of accessing and modifying the actual parameters. Later in this tutorial, we will explore a practical example of the Call by Address technique.
Example for Call by address:
Let's consider an example to demonstrate Call by Reference in C++.
#include <iostream>
using namespace std;
void cba(int *j)
{
cout << endl << ?In the cba() function : " << endl;
cout << "Value of *j = " << *j << endl;
*j = 10;
cout << "Value of *j = " << *j << endl;
}
int main()
{
int t = 5;
cout << "Value of t inside main() = " << var << endl;
cba(&t);
cout << endl << "Value of t inside main() = " << var << endl;
return 0;
}
Output:
Value of t inside main() = 5
In the cba() function :
Value of *j = 5
Value of *j = 10
Value of t inside main() = 10
Explanation:
In this instance, we will demonstrate the Call by reference mechanism. We invoke the "cba" function and provide the reference of "t" from the "main" function. Within the function definition, the reference of "t" is stored in a pointer variable, denoted as "j". This pointer is employed in the hello function to modify the value of "t" to 10. Consequently, after executing the "cba" function, the value of "t" is updated to 10 within the "main" function.
Example 2:
Let's consider another instance to demonstrate the Call by Reference in C++.
#include <iostream>
using namespace std;
void mySwap(int *p_1, int *p_2)
{
int var;
var = *p_1;
*p_1 = *p_2;
*p_2 = var;
}
int main()
{
int v1 = 5;
int v2 = 10;
cout<<"Before swapping, value of v1 : " <<v1<< endl;
cout<< "Before swapping, value of v2 : "<<v2<< endl;
mySwap(&v1, &v2);
cout<<"After swapping, value of v1 : "<<v1<<endl;
cout<<"After swapping, value of v2 : "<<v2<<endl;
return 0;
}
Output:
Explanation:
In this instance, we will showcase how the Call by reference technique can be utilized to address a practical problem. To exemplify, let's consider a scenario where we need to develop a function that exchanges the values of two variables. When two variables are interchanged using the Call by value method, the original variables remain unaffected in the calling function. To address this, we can employ the Call by reference method. In the following scenario, the "mySwap" function is provided with the memory addresses of both v1 (&v1) and v2 (&v2). By utilizing pointers to switch the values of these variables within the "mySwap" function, we can observe from the subsequent result that upon calling the "mySwap" function, the actual values of these variables are interchanged in the "main" function.
Advantages of Call by address in C++:
There are several advantages of Call by Address in C++. Some main advantages of Call by Address are as follows:
- Efficiency: Passing huge data structures by reference or pointer rather than by value can be more effective when doing so for objects or arrays. It doesn't require making a copy of the data, which would be more work.
- Original Data Modification: Any modifications made to the parameter inside the function will have a direct impact on the original data when we send an argument by reference or pointer. When we want to change the original data inside a function, it is helpful.
- Avoiding "Object Slicing": When working with polymorphism and inheritance, passing objects by value might result in "object slicing" , in which only the base class portion of the object is duplicated, losing the derived class-specific data. This problem is avoided by passing objects by reference or pointer.
- Flexibility: Function arguments can be more freely tailored due to the references and pointers. Using pointers or references, we can send many kinds of objects and data structures to a single function.
- Passing Null or Optional Values: In some programming circumstances, pointers can be set to nullptr or used as optional parameters to express the absence of a value.
- Multiple Return Values: By altering the values referenced or pointed to, pointers and references can be utilized to return numerous values from a function.
Nevertheless, it is essential to manage references and pointers with caution as mishandling them can lead to issues such as null pointer dereferencing, memory leaks, and unintended consequences. When deciding between utilizing Call by Reference or Call by Value in C++, it is imperative to prioritize the design and security of our code.