Call by value and call by reference in C
Introduction
Have you ever wondered what the difference is between method calls made using call by value and those done with call by reference? If so, you have come to the right place. In this article, we will be looking at call by value and call by reference specifically in C programming language. We'll explore how these methods are used, their advantages and disadvantages, as well as any other pertinent information that can help you better understand the concepts behind each approach. By the time you finish reading this article, not only will you have a good understanding of both techniques but also feel confident enough to apply them wherever necessary!
What is call by value in C?
Call by value in C is a fascinating approach to passing arguments to functions in the C programming language, where the value of an actual parameter is passed to the corresponding formal parameter of the called function. This technique ensures that the original data cannot be altered by the called function, as it deals only with a copy of that data. When a programmer opts for call by value, the memory space for the formal parameters is allocated separately in the function stack frame, which further reinforces the fact that the original data remains untouched. The concept of call by value plays a vital role in maintaining the integrity and stability of programs, allowing developers to create more efficient and reliable applications.
Example
#include <stdio.h>
void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
int main() {
int a = 40, b = 50;
printf("Before swap, a = %d and b = %d\n", a, b);
swap(a, b);
printf("After swap, a = %d and b = %d\n", a, b);
return 0;
}
Before swap, a = 40 and b = 50
After swap, a = 50 and b = 40
What is call by reference in C?
Example
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5;
int y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
Output
Before swap: x = 5, y = 10
After swap: x = 10, y = 5
Differences between call by value and call by reference in C
Properties | Call by value | Call by reference |
Data copying | In call by value, a copy of the value of the argument is passed to the function | In call by reference, the memory address of the argument is passed to the function. |
Original value modification | In call by value, any changes made to the parameter inside the function do not affect the original value passed to the function | In call by reference, any changes made to the parameter inside the function affect the original value passed to the function. |
Memory usage | Call by value needs more memory | Call by reference uses less memory |
Performance | Call by value is generally faster than call by reference | Call by reference is slower than call by value |
Function definition | In call by value, the function definition does not require any special syntax | In call by reference, the parameter must be declared as a pointer in the function definition. |
Summary
In conclusion, call by value and call by reference in C is an important concepts to understand if you’re intent on developing a deeper conceptual understanding of computer programming. Call by value partially deep copies a variable whereas call by reference passes a pointer instead. Depending on what the output is supposed to be, developers need to decide which one they should use. Knowing the difference between these two calls is essential for programming effectively in C. While there’s quite a lot more to learn about computer science and C programming, mastering this concept is the first step to becoming truly efficient in a language like C. If you're looking to improve your coding skill, it would benefit you greatly to focus your attention on understanding both calls better and implementing them in your projects as needed.
Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.