Call by value and Call by reference in C | Difference between call by value and call by reference in c

 

Call by value and Call by reference in C

There are two methods to pass the data into the function in C language, i.e., call by value and call by reference.

Call by value and Call by reference in C


1. Call by value in C

In call by value, a copy of the actual argument's value is passed to the function. Any changes made to the parameter inside the function do not affect the original argument.
c
#include <stdio.h> void modifyValue(int x) { x = x * 2; printf("Inside function: x = %d\n", x); } int main() { int num = 5; printf("Before function call: num = %d\n", num); modifyValue(num); printf("After function call: num = %d\n", num); return 0; }

Output:


Before function call: num = 5 Inside function: x = 10 After function call: num = 5
As you can see, the modifyValue function works with a copy of the num variable. Changes made inside the function do not affect the original num variable.

Call by Value Example: Swapping the values of the two variables

Here's an example of swapping two variables using call by value:
c
#include <stdio.h> void swap(int a, int b) { int temp = a; a = b; b = temp; } int main() { int x = 5, y = 10; printf("Before swapping: x = %d, y = %d\n", x, y); // Call the swap function with call by value swap(x, y); printf("After swapping: x = %d, y = %d\n", x, y); return 0; }

Output:


Before swapping: x = 5, y = 10 
After swapping: x = 5, y = 10

In this example, the swap function is defined to accept two integers a and b as parameters. Inside the function, we use a temporary variable temp to swap the values of a and b. However, notice that when we call the swap function from main, the values of x and y do not change because the function works with copies of the original values due to call by value.

2. Call by Reference (Using Pointers):

In C, there is no true "call by reference" like in some other languages, but you can simulate it using pointers. By passing a pointer to a variable, you can modify the original variable through that pointer
c
#include <stdio.h> void modifyReference(int *x) { *x = *x * 2; printf("Inside function: *x = %d\n", *x); } int main() { int num = 5; printf("Before function call: num = %d\n", num); modifyReference(&num); printf("After function call: num = %d\n", num); return 0; }

Output:


Before function call: num = 5 Inside function: *x = 10 After function call: num = 10
In this example, we pass a pointer to num to the modifyReference function. Inside the function, we dereference the pointer using *x to modify the original variable, which affects num outside the function as well.

Call by reference Example: Swapping the values of the two variables

Here's an example of swapping two variables using call by reference:
c
#include <stdio.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int x = 5, y = 10; printf("Before swapping: x = %d, y = %d\n", x, y); // Call the swap function with call by reference (passing pointers) swap(&x, &y); printf("After swapping: x = %d, y = %d\n", x, y); return 0; }

Output:


Before swapping: x = 5,
y = 10 After swapping: x = 10, y = 5
In this example, the swap function accepts two integer pointers a and b as parameters. Inside the function, we use these pointers to access and modify the values of x and y, effectively swapping their values.

Difference between call by value and call by reference in c


No.Call by valueCall by reference
1A copy of the value is passed into the functionAn address of value is passed into the function
2Changes made inside the function is limited to the function only. The values of the actual parameters do not change by changing the formal parameters.Changes made inside the function validate outside of the function also. The values of the actual parameters do change by changing the formal parameters.
3Actual and formal arguments are created at the different memory locationActual and formal arguments are created at the same memory location

Post a Comment

0 Comments