Call by Value and Call by Reference in C
Hello Everyone, In this article, we will cover the concept of Call by Value and Call by Reference in C Programming. Understanding these concepts will help you grasp how functions handle data in C. We will explain how each method works, provide examples, and highlight the differences between them. Whether you’re new to C or have some experience, this guide will help you to understand Call by Value and Call by Reference in C effectively. Let’s get started.
In C programming, there are two primary ways to pass arguments to a function: call by value and call by reference. Here’s an explanation of both, along with code examples:
1. Call by Value
In call by value, the value of the actual parameter is passed to the formal parameter of the function. The formal parameter acts as a local variable within the function, and any changes made to it do not affect the actual parameter.
Example:
#include <stdio.h>
void update_value(int value) {
value = value + 10;
printf("Value inside the function: %d\\n", value);
}
int main() {
int num = 5;
printf("Value before function call: %d\\n", num);
update_value(num);
printf("Value after function call: %d\\n", num);
return 0;
}
Output:
Value before function call: 5
Value inside the function: 15
Value after function call: 5
As you can see, the value of ‘num’ remains the same after the function call, because only a copy of its value was passed to the function.
Here are two code examples to further explain the concept of call by value in C programming, with comments added to make the code more informative:
Doubling an integer value using call by value
#include <stdio.h>// Function to double the input value
int double_value(int number) {
return number * 2;
}
int main() {
int num = 10;
printf("Original value: %d\\n", num);
// Call double_value function by value and store the result
int doubled_num = double_value(num);
printf("Doubled value: %d\\n", doubled_num);
return 0;
}
Output:
Original value: 10
Doubled value: 20
In this example, the double_value function receives a copy of the input integer num and doubles it. Since it’s a call by value, the original value of num remains unchanged.
C Program to calculate the square of an integer using call by value:
#include <stdio.h>// Function to calculate the square of the input value
int square(int number) {
return number * number;
}
int main() {
int num = 5;
printf("Original value: %d\\n", num);
// Call square function by value and store the result
int squared_num = square(num);
printf("Squared value: %d\\n", squared_num);
return 0;
}
Output:
Original value: 5
Squared value: 25
In this example, the square function receives a copy of the input integer num and calculates its square. Since it’s a call by value, the original value of num remains unchanged.
2. Call by Reference
In call by reference, the address of the actual parameter is passed to the formal parameter. This allows the function to directly modify the value of the actual parameter.
Example:
#include <stdio.h>
void update_value(int *value_ptr) {
*value_ptr = *value_ptr + 10;
printf("Value inside the function: %d\\n", *value_ptr);
}
int main() {
int num = 5;
printf("Value before function call: %d\\n", num);
update_value(&num);
printf("Value after function call: %d\\n", num);
return 0;
}
Output:
Value before function call: 5
Value inside the function: 15
Value after function call: 15
In this example, the address of ‘num’ is passed to the function, allowing it to directly modify the value of ‘num’. The value of ‘num’ is updated after the function call.
C Program to swap the two number using call by reference
#include <stdio.h>
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1 = 10, num2 = 20;
printf("Before swapping: num1 = %d, num2 = %d\\n", num1, num2);
swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\\n", num1, num2);
return 0;
}
Output:
Before swapping: num1 = 10, num2 = 20
After swapping: num1 = 20, num2 = 10
In this example, the swap function accepts the addresses of two integers as its arguments. It then uses these pointers to directly modify the values of the input integers, effectively swapping their values.
C Program to calculate the area and perimeter of a rectangle by using call by reference to return multiple values from a function
#include <stdio.h>
void rectangle_properties(int length, int width, int *area, int *perimeter) {
*area = length * width;
*perimeter = 2 * (length + width);
}
int main() {
int length = 5, width = 3;
int area, perimeter;
rectangle_properties(length, width, &area, &perimeter);
printf("Area of rectangle: %d\\n", area);
printf("Perimeter of rectangle: %d\\n", perimeter);
return 0;
}
Output:
Area of rectangle: 15
Perimeter of rectangle: 16
In this example, the rectangle_properties function calculates the area and perimeter of a rectangle. It accepts the length and width of the rectangle as its first two arguments and the addresses of two integer variables for the area and perimeter. The function directly modifies the values of the input area and perimeter variables using the provided pointers.