What is the Difference Between Call By Address and Call By Reference

The main difference between Call By Address and Call By Reference is that in the call by address, the address of an argument copies to the formal parameter of the function while, in the call by reference, the reference of an argument copies to the formal parameter of the function.

Generally, a function is a set of statements that allows performing a task multiple times. A function helps to make the program more readable. In programming languages, such as C++, the execution starts from the main method, and it is possible to call the other functions or methods from the main method. These languages have various methods to call a function. Two such methods in C++ are “call by address” and “call by reference”.

Key Areas Covered

1. What is Call By Address
     -Definition, Functionality
2. What is Call By Reference
     -Definition, Functionality
3. Difference Between Call By Address and Call By Reference
     -Comparison of key differences

Key Terms

C++, Call By Address, Call By Pointers, Call By Reference, Function, Pointer, Reference

Difference Between Call By Address and Call By Reference - Comparison Summary

What is Call By Address

Call By Address is also known as Call By Pointers. In this method, the programmer passes the addresses of the actual arguments to the formal parameters. Then, the function uses the addresses to access the actual arguments. In other words, the changes made to the formal parameters affect the actual arguments. To pass the value by pointer, the argument pointers are passed to the functions similar to any other value. An example program is as follows.

Difference Between Call By Address and Call By Reference

Figure 1: C++ program with call by address

There are two integers in the main program: a’ and ‘b’. The addresses of ‘a’ and ‘b’ are passed to the function. So, the function gets these addresses. And, the temp variable helps to swap the two values. Then, the function operates on the actual values using the addresses. Therefore, we can observe that the changes made inside the function are visible in the main program. Finally, we can see that the two values are swapped. Now ‘a’ has 20, and ‘b’ has 10.

What is Call By Reference

Before understanding the concept of call by reference, it is important to understand the concept of reference variables. Assume the below code.

int a = 10;

int &b= a;

Here, b is a reference variable. These variables do not occupy any memory space of its own. Therefore, it shares the memory of the already allocated variable. In other words, b shares the memory of a. When the programmer changes the actual variable, the reference variable also changes. If the programmer changes a, it reflects in b, and if he changes b, then a will use that new value of b. Likewise, reference variables help to save memory.

In call by reference, the references of the arguments copy to the formal parameters. Inside the function, the programmer can use the references to access the actual arguments. Therefore, the changes made to the parameter affect the actual argument. Furthermore, to pass the value by reference, argument reference is passed to the functions just like any other value.

Main Difference - Call By Address vs Call By Reference

Figure 2: C++ program with call by reference

In the main program, there are two integers in the main program: ‘a’ and ‘b’. Those two values are passed to the swap function. In the swap function, p and q are formal parameters. The &p and &q indicate that they are reference variables. Then the swap function makes changes to the values. Finally, the swapped values display on the console.

Difference Between Call By Address and Call By Reference

Definition

Call By Address is a way of calling a function in which the address of the actual arguments is copied to the formal parameters. But, call by reference is a method of passing arguments to a function by copying the reference of an argument into the formal parameter. Thus, this is the difference between Call By Address and Call By Reference.

Functionality

Moreover, another difference between Call By Address and Call By Reference is their functionality. In the call by address, the programmer passes the addresses of the actual arguments to formal parameters while, in the call by reference, the programmer passes the references of the actual arguments to the formal parameters.

Memory allocation

Besides, in the call by address, the memory is allocated for both actual arguments and formal parameters whereas, in the call by reference, the memory is allocated only for actual arguments and formal parameters share that memory. 

Conclusion

In brief, Call By Address and Call By Reference are two methods of passing values to a function. The main difference between Call By Address and Call By Reference is that in the call by address, the address of an argument copies to the formal parameter of the function while, in the call by reference, the reference of an argument copies to the formal parameter of the function.

References:

1.Gupta, Sanjay. 07 C Call By Value, Call by Address and Call by Reference Methods of Function Call, YouTube, 3 Jan. 2017, Available here.
2.Tutorialspoint.com. “C Function Call by Pointer.” Www.tutorialspoint.com, Available here.
3.Tutorialspoint.com. “C Function Call by Reference.” Www.tutorialspoint.com, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply