Call by Value and Call by Reference in C

When we pass arguments to a function in C, there are two common ways to do it: Call by Value and Call by Reference.

Understanding these concepts is very important because they control how data is shared between functions.

1. Call by Value

In Call by Value, a copy of the actual argument is passed to the function. Any changes made inside the function do NOT affect the original variable.

How It Works

• The function receives a duplicate copy of the variable.

• Modifications are done on the copy.

• Original value remains unchanged.

main() → value copied → function

Example Program: Call by Value

C
C program demonstrating call by value
#include <stdio.h>

void swap(int a, int b) {
    int temp;
    temp = a;
    a = b;
    b = temp;
    printf("Inside function: a = %d, b = %d\n", a, b);
}

int main() {
    int x = 10, y = 20;
    
    printf("Before function call: x = %d, y = %d\n", x, y);
    
    swap(x, y);
    
    printf("After function call: x = %d, y = %d\n", x, y);
    
    return 0;
}
Output:
Before function call: x = 10, y = 20
Inside function: a = 20, b = 10
After function call: x = 10, y = 20

Explanation: Even though values are swapped inside the function, the original variables x and y remain unchanged.

2. Call by Reference

In Call by Reference, the address of the variable is passed to the function. This allows the function to modify the original variable.

In C, call by reference is implemented using pointers.

How It Works

• The function receives the memory address of the variable.

• Changes are made directly to the original variable.

main() → address passed → function → original modified

Example Program: Call by Reference

C
C program demonstrating call by reference using pointers
#include <stdio.h>

void swap(int *a, int *b) {
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 10, y = 20;
    
    printf("Before function call: x = %d, y = %d\n", x, y);
    
    swap(&x, &y);
    
    printf("After function call: x = %d, y = %d\n", x, y);
    
    return 0;
}
Output:
Before function call: x = 10, y = 20
After function call: x = 20, y = 10

Explanation: Since we passed the addresses of x and y, the swap function modified the original variables.

3. Difference Between Call by Value and Call by Reference

Call by Value:
- Copy of variable is passed
- Original data not changed
- Uses normal parameters

Call by Reference:
- Address of variable is passed
- Original data is changed
- Uses pointers

4. When to Use?

Use Call by Value when you do not want the function to modify original data.

Use Call by Reference when you want the function to modify original variables or when passing large data structures to improve efficiency.

Conclusion

Call by Value passes a copy of the data, so changes do not affect the original variable.

Call by Reference passes the address of the variable using pointers, allowing the function to modify the original value.

Understanding these concepts is essential for mastering functions and pointers in C programming.