Difference Between Call by Value and Call by Reference in C

1. Introduction

In C programming, understanding the difference between call by value and call by reference is crucial for manipulating data in functions. Call by value means calling a function with actual values of arguments, whereas call by reference means calling a function with addresses of arguments.

2. Key Points

1. Call by value copies the actual value of an argument into the formal parameter of the function.

2. In call by value, changes made inside the function are not reflected in the actual arguments.

3. Call by reference passes the address of the argument into the function, effectively referencing the original variable.

4. In call by reference, changes made inside the function are reflected in the actual arguments.

3. Differences

Call by Value Call by Reference
Passes a copy of the variable's value. Passes the variable's address in memory.
Function changes do not affect the original variable. Function changes directly affect the original variable.
Safer and more isolated, as it doesn’t affect the original data. More efficient for large data, as it doesn't create copies.

4. Example

#include <stdio.h>

// Example of call by value
void modifyValue(int data) {
    data = 20;
    printf("Inside modifyValue: %d\n", data);
}

// Example of call by reference
void modifyReference(int *data) {
    *data = 20;
    printf("Inside modifyReference: %d\n", *data);
}

int main() {
    int value = 10;
    int reference = 10;

    modifyValue(value);
    printf("After modifyValue: %d\n", value);

    modifyReference(&reference);
    printf("After modifyReference: %d\n", reference);

    return 0;
}

Output:

Inside modifyValue: 20
After modifyValue: 10
Inside modifyReference: 20
After modifyReference: 20

Explanation:

1. In call by value, the modifyValue function is unable to change the original value of value in main.

2. In call by reference, the modifyReference function changes the original value of reference in main through the pointer.

5. When to use?

- Use call by value when you want to protect the original data from being altered by the function.

- Use call by reference when you need to modify the original data or are working with large data structures where copying would be inefficient.

Difference between malloc() and calloc()?

Difference between Local Variable and Global Variable in C

Difference between Global and Static Variables in C

Difference Between Call by Value and Call by Reference in C

Difference Between getch() and getche() in C

Difference between printf() and sprintf() in C

Difference between Arrays and Pointers in C

Difference between Structure and Union in C

Difference Between Stack and Heap Memory Allocation in C

Difference Between Macro and Function in C

Difference between = and == in C

Difference Between for loop and while loop in C

Difference Between Linked List and Array in C

Difference between fgets() and gets() in C

Difference between ++i and i++ in C

Difference between struct and typedef struct in C

Difference between int main() and void main() in C

Difference between Character Array and String in C

Difference between break and continue in C

Difference between exit() and return in C

Comments