Call by value and call by reference in C

Sakshi Dhameja  5 min read
12 Apr 2023
Intermediate
125 Views

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;
}

Output
Before swap, a = 40 and b = 50
After swap, a = 50 and b = 40

What is call by reference in C?

Call by reference in C programming plays a pivotal role in enhancing the functionality and efficiency of the code. Instead of simply passing the value of a variable to a function, call by reference allows the programmer to share the memory address of that variable with the process. Consequently, any modifications made by the function directly impact the original variable, ensuring its updated value is consistently accessible throughout the program. This technique not only conserves memory but also simplifies collaboration, as complex data types like arrays or structures can easily be manipulated without being duplicated needlessly. Ultimately, mastering call by reference in C programming paves the way for developing robust and streamlined applications.

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

PropertiesCall by valueCall by reference
Data copyingIn call by value, a copy of the value of the argument is passed to the functionIn call by reference, the memory address of the argument is passed to the function.
Original value modificationIn call by value, any changes made to the parameter inside the function do not affect the original value passed to the functionIn call by reference, any changes made to the parameter inside the function affect the original value passed to the function.
Memory usageCall by value needs more memoryCall by reference uses less memory
PerformanceCall by value is generally faster than call by referenceCall by reference is slower than call by value
Function definitionIn call by value, the function definition does not require any special syntaxIn 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.

Accept cookies & close this