0
0
CComparisonBeginner · 4 min read

Call by Value vs Call by Reference in C: Key Differences and Examples

In C, call by value passes a copy of the variable to a function, so changes inside the function do not affect the original variable. Call by reference passes the variable's address (pointer), allowing the function to modify the original variable directly.
⚖️

Quick Comparison

Here is a quick comparison of call by value and call by reference in C:

FactorCall by ValueCall by Reference
What is passed?A copy of the variable's valueThe address (pointer) of the variable
Effect on original variableNo changeCan be changed
Memory usageUses extra memory for copyUses less memory, passes address
SafetySafer, original data protectedLess safe, can modify original data
Use caseWhen original data should not changeWhen function needs to modify original data
SyntaxSimple variablePointer variable
⚖️

Key Differences

Call by value means the function receives a copy of the variable's value. Any changes made inside the function affect only the copy, so the original variable outside the function remains unchanged. This is simple and safe because the original data is protected from accidental modification.

In contrast, call by reference means the function receives the address of the variable using pointers. The function can then directly access and modify the original variable's value. This allows functions to change variables outside their own scope but requires careful handling of pointers to avoid errors.

Because C does not support call by reference natively, programmers simulate it by passing pointers. This difference affects how you write and understand functions that need to modify variables passed to them.

⚖️

Code Comparison

This example shows call by value where the function tries to change a variable but the original stays the same.

c
#include <stdio.h>

void addTen(int num) {
    num = num + 10;
    printf("Inside function: %d\n", num);
}

int main() {
    int x = 5;
    addTen(x);
    printf("Outside function: %d\n", x);
    return 0;
}
Output
Inside function: 15 Outside function: 5
↔️

Call by Reference Equivalent

This example shows call by reference using pointers to modify the original variable.

c
#include <stdio.h>

void addTen(int *num) {
    *num = *num + 10;
    printf("Inside function: %d\n", *num);
}

int main() {
    int x = 5;
    addTen(&x);
    printf("Outside function: %d\n", x);
    return 0;
}
Output
Inside function: 15 Outside function: 15
🎯

When to Use Which

Choose call by value when you want to protect the original data from changes and only need to use the value inside the function. It is simpler and safer for read-only operations.

Choose call by reference when the function needs to modify the original variable or when passing large data structures to avoid copying overhead. It is essential for functions that must update multiple values or return results via parameters.

Key Takeaways

Call by value passes a copy, so original variables remain unchanged.
Call by reference passes the address, allowing functions to modify original variables.
Use call by value for safety and simplicity when no modification is needed.
Use call by reference to modify variables or improve performance with large data.
In C, call by reference is done using pointers explicitly.