In the C programming language, functions play a major role in modular and reusable code. When a function is called, data can be passed to it in two ways — Call by Value and Call by Reference.
Call by Value and Call by Reference in C
Introduction
Call by Value in C
When we pass the value of a variable to a function, it is known as Call by Value.
In this method, the actual parameter’s value is copied to the formal parameter. Hence, any modification made inside the function does not affect the original value of the variable because both variables are stored in different memory locations.
Program: Calculate the Perimeter of a Rectangle
Program: Calculate the Perimeter of a Rectangle
#include <stdio.h>
int perimeter(int l, int b); // Function declaration
void main()
{
int n1 = 5, n2 = 10;
int n3 = perimeter(n1, n2); // Function call
printf(“Perimeter of rectangle is: %d”, n3);
}
int perimeter(int l, int b) // Function definition
{
int p = 2 * (l + b);
return p;
}
Output:
Perimeter of rectangle is: 30
Advantages of Call by Value
- The original variable’s value remains unchanged, ensuring data safety.
- Changes made inside the function do not affect the actual parameters.
- Ideal when you want to prevent unintended modification of data.
Disadvantages of Call by Value
- It is less memory efficient because values are duplicated.
- You cannot modify the original variable inside the function.
- Creates overhead when passing large data structures.
When to Use Call by Value
- When the original data must remain unchanged.
- When a function only needs to read data, not modify it.
- When working with small variables or simple calculations.
Call by Reference in C
When we pass the address of a variable to a function instead of its value, it is called Call by Reference or Call by Address.
In this method, the formal parameter becomes a pointer variable that stores the address of the actual parameter. Thus, any changes made through the pointer directly modify the original variable since both refer to the same memory location.
Program: Swapping Two Variables Using Call by Reference
Program: Swapping Two Variables Using Call by Reference
#include <stdio.h>
void swap(int *p, int *q); // Function declaration
void main()
{
int n1 = 50;
int n2 = 60;
printf(“Before swap: n1 = %d, n2 = %d\n”, n1, n2);
swap(&n1, &n2); // Function call with address
printf(“After swap: n1 = %d, n2 = %d\n”, n1, n2);
}
void swap(int *p, int *q) // Function definition
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
Output:
Before swap: n1 = 50, n2 = 60
After swap: n1 = 60, n2 = 50
Advantages of Call by Reference
- No duplicate memory is used since the same address is referenced.
- Allows modification of the original variable inside the function.
- Efficient for passing large data structures like arrays and structures.
Disadvantages of Call by Reference
- Changes made in the function affect the original variable, which can cause unintended results.
- Makes debugging more complex if multiple functions modify shared data.
When to Use Call by Reference
- When you want the function to modify the original variable.
- When passing large data structures (to save memory).
- When multiple values need to be returned from a single function.
Difference Between Call by Value and Call by Reference
| Call by Value | Call by Reference |
| The value of the variable is copied and passed to the function. | The address of the variable is passed to the function. |
| Changes inside the function do not affect the original variable. | Changes inside the function affect the original variable. |
| Actual and formal parameters are stored in different memory locations. | Actual and formal parameters share the same memory location. |
| Safer because the original data cannot be modified accidentally. | Riskier because the original data can be modified. |
| Less efficient for large data since values are copied. | More efficient for large data since only addresses are passed. |
| Uses normal variables. | Uses pointer variables. |
Conclusion
Both Call by Value and Call by Reference are essential concepts in C programming.
- Use Call by Value when you want data protection and no modification of the original variable.
- Use Call by Reference when you need to modify the original data or work with large data structures efficiently.
Understanding these methods helps improve your control over memory management and program efficiency in C.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining