C Call by Value and Call by Reference

In C, there are two ways to pass arguments to functions: call by value and call by reference. Understanding the difference between these two methods of argument passing is essential for effectively using functions in C.

In this article, we will explore call by value and call by reference in C, provide two examples to demonstrate their use, and show the outputs of the examples.

Call by Value

In call by value, the actual argument is passed to the function, and a copy of the value is created. Any changes made to the argument within the function do not affect the original value. The following example demonstrates call by value in C:

1#include <stdio.h> 2 3void swap(int x, int y) { 4 int temp; 5 temp = x; 6 x = y; 7 y = temp; 8} 9 10int main() { 11 int a = 100, b = 200; 12 13 printf("Before swap, value of a : %d\n", a ); 14 printf("Before swap, value of b : %d\n", b ); 15 16 swap(a, b); 17 18 printf("After swap, value of a : %d\n", a ); 19 printf("After swap, value of b : %d\n", b ); 20 21 return 0; 22}

The output of this program is:

1Before swap, value of a : 100 2Before swap, value of b : 200 3After swap, value of a : 100 4After swap, value of b : 200

In this example, the swap function takes two integers, x and y, as inputs and swaps their values. However, when the function returns, the values of a and b in the main function remain unchanged. This is because the arguments passed to the function are copies of the original values, and any changes made to the arguments within the function do not affect the original values.

Call by Reference

In call by reference, the address of the actual argument is passed to the function. The function then accesses the value stored at that address and can modify it, effectively modifying the original value. The following example demonstrates call by reference in C:

1#include <stdio.h> 2 3void swap(int *x, int *y) { 4 int temp; 5 temp = *x; 6 *x = *y; 7 *y = temp; 8} 9 10int main() { 11 int a = 100, b = 200; 12 13 printf("Before swap, value of a : %d\n", a ); 14 printf("Before swap, value of b : %d\n", b ); 15 16 swap(&a, &b); 17 18 printf("After swap, value of a : %d\n", a ); 19 printf("After swap, value of b : %d\n", b ); 20 21 return 0; 22}

The output of this program is:

1Before swap, value of a : 100 2Before swap, value of b : 200 3After swap, value of a : 200 4After swap, value of b : 100

In this example, the swap function takes two pointers to integers, x and y, as inputs. The &operator is used to pass the addresses of the variablesaandbin themainfunction to the functionswap. This allows the function to access and modify the values stored at those addresses, effectively swapping the values of aandb.

The difference between call by value and call by reference can be seen in the output of the two examples. In the call by value example, the values of a and b in the main function remained unchanged after the swap function was called. However, in the call by reference example, the values of a and b were swapped after the function was called.

It's worth noting that while call by reference can be more efficient and allow for more complex operations, it also comes with some additional considerations. For example, passing a pointer to a variable that is dynamically allocated with malloc can result in memory leaks if the pointer is not properly used.