Call by address in C++

Call By Address is also referred to as Call By Pointers. In this Call By Address method, the developer passes the actual arguments addresses to the formal parameters. After that, the function utilizes these addresses to access the actual arguments in the system. In other words, the function arguments are passed as address when utilizing the Call by address/Pass by address technique. The calling function passes the address of the parameters. There are pointer variables utilized in the definition of the function. The function can access and modify the actual parameters with the aid of the Call by address mechanism. In a later portion of this tutorial, we'll see an illustration of the Call by address method.

Example for Call by address:

Let's take an example to illustrate the Call by Address in C++.

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 example, we will illustrate the Call by address mechanism. We are calling the "cba()" function and passing the address of "t" from the "main()" function. In the function definition, we are given the address of "t" in a pointer variable, or "j". The pointer is used within the function hello to alter the value of "t" to 10. As a result, following the "cba()" function call, the value of "t" is set to 10 inside the "main()" function.

Example 2:

Let's take another example to illustrate the Call by Address in C++.

Output:

Call by address in C++

Explanation:

In this example, we'll demonstrate how the Call by address method can be used to a practical issue. As an illustration, we wish to create a function that switches two variables. The real variables do not change in the caller function if we swap two variables using the Call by value approach. In this case, the Call by address approach can be applied. In this example, the "mySwap()" function is being sent the addresses of both v1 (&v1) and v2 (&v2). We are using the pointers to swap the values of these two variables inside the "mySwap()" function. As we can see in the output below, after the "mySwap()" function has been called, the real values of these variables are swapped in the "main()" method.

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. Multiple Return Values: By altering the values referenced or pointed to, pointers and references can be utilized to return numerous values from a function.

However, it's crucial to handle references and pointers carefully because if not managed properly, they can result in problems like null pointer dereferencing, memory leaks, and unwanted side effects. When selecting whether to use Call by reference or call by value in C++, we should always take the design and safety of our code into account.


Next TopicChrono in C++