What is the Difference Between Call by Value and Call by Address

The main difference between call by value and call by address is that, in call by value, the values of the actual parameters copy to the formal parameters of the function while in call by address, the addresses of the actual parameters copy to the formal parameter of the function.

A function is a set of statements that performs a certain task multiple times. Rather than writing all the program statements inside the main method, we can divide the program into several functions and call them when necessary. Also, this makes the program reusable and easier to read. There are two types of arguments related to functions: actual and formal parameters. Actual parameters are the values that pass to the function when invoking it while formal parameters are the variable inside the functions that receive values when calling the function. Two methods to call a function in programming languages such as ‘C’ are call by value and call by address.

Key Areas Covered

1. What is Call by Value
     – Definition, Functionality
2. What is Call by Address
     – Definition, Functionality
3. What is the Difference Between Call by Value and Call by Address
     – Comparison of Key Differences

Key Terms

Call by Value, Call by Address, Functions

Difference Between Call by Value and Call by Address - Comparison Summary

What is Call by Value

A function performs the defined task and returns the answer. If the return type is void, it will perform the task and return no value. In call by value, the actual arguments are copied to the formal parameters of the function. The function uses the values in the formal parameters, not the actual parameters. Therefore, the original values do not change. An example is as follows.

Difference Between Call by Value and Call by Address

Figure 1: Program with call by value

In the above example, there are two integer values in the main method as ‘a’ and ‘b’. The swap function gets these two values. Inside the function, the ‘a’ value is copied to x and the b value is copied to y. Thus, x is 10 and y is 20. Inside the function, the temp variable helps to swap these two values. Now x is 20 and y is 10. After executing the function, the control passes back to the main function. When printing ‘a’ and ‘b’, it prints the original values, i.e., 10 for ‘a’ and 20 for ‘b’. Thus, this is call by value. In this, the actual value does not change. But, the change only reflects inside the function.

What is Call by Address

Another name for call by address is call by pointers. Call by address method copies the addresses of the arguments into formal parameters. The function uses the addresses to access the actual values. Therefore, the changes made inside the function reflect in the original values. An example is as follows.

Main Difference - Call by Value vs Call by Address

Figure 2: Program with call by Address

In the above example, there are two integers in the main program as ‘a’ and ‘b’. The addresses of ‘a’ and ‘b’ pass to function. The function gets these addresses. The temp variable helps to swap the two values. As the function operates on the actual values using the addresses, the changes inside the function are visible in the main program. Therefore, even though the actual ‘a’ and ‘b’ values are 10 and 20, now the ‘a’ value is 20 and ‘b’ value is 10.

Difference Between Call by Value and Call by Address

Definition

Call by value is a way of passing arguments to a function by copying the actual values of arguments into formal parameters of the function while Call by pointer is a way of passing arguments to a function by copying the addresses of the arguments to the formal parameters of the function.

Functionality

In call by value, the values of the actual parameters copy to the formal parameters of the function. However, in the call by address, the addresses of the actual parameters copy to the formal parameter of the function. Thus, functionality is the main difference between call by value and call by address. 

Effect on Original Values

Another major difference between call by value and call by address is their effect on original values. Call by value does not affect the original values whereas Call by address affects the original values.

Conclusion

The main difference between call by value and call by address is that, in call by value, the values of the actual parameters copy to the formal parameters of the function while the call by address, the addresses of the actual parameters copy to the formal parameter of the function. In brief, call by value does not change the original value while call by address changes the original values.

Reference:

1. “C Functions.” Www.tutorialspoint.com, Tutorials Point, 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