Call by value and Call by reference in C

In C, when passing arguments to a function, there are two commonly used methods: call by value and call by reference.

  1. Call by Value: In call by value, the function receives a copy of the value passed as an argument. Any modifications made to the parameter inside the function do not affect the original value outside the function. This is the default method of passing arguments in C. Here’s an example:
#include <stdio.h>

void changeValue(int num) {
    num = 10;  // Modifying the parameter
}

int main() {
    int num = 5;
    printf("Before function call: %d\n", num);
    changeValue(num);
    printf("After function call: %d\n", num);
    return 0;
}

Output:

Before function call: 5
After function call: 5

In the above example, the changeValue function receives a copy of the num variable. Modifying num inside the function does not affect the original value of num in the main function. Thus, the output remains unchanged.

  1. Call by Reference: In call by reference, the function receives the address of the variable as the argument. By using pointers, any modifications made to the parameter inside the function affect the original value outside the function. Here’s an example:
#include <stdio.h>

void changeValue(int* numPtr) {
    *numPtr = 10;  // Modifying the value at the address stored in numPtr
}

int main() {
    int num = 5;
    printf("Before function call: %d\n", num);
    changeValue(&num);  // Passing the address of num
    printf("After function call: %d\n", num);
    return 0;
}

Output:

Before function call: 5
After function call: 10

In this example, the changeValue function receives a pointer to num as an argument. By dereferencing the pointer (*numPtr), the value at the address stored in numPtr can be modified. As a result, the original num variable is changed when the function is called, as observed in the output.

By understanding the difference between call by value and call by reference, you can choose the appropriate method based on your requirements when working with functions in C.

Difference between call by value and call by reference in c:

The main differences between call by value and call by reference in C are as follows:

  1. Method of Passing Arguments:
    • Call by Value: In call by value, the function receives a copy of the value passed as an argument. The original value is not affected by any modifications made to the parameter inside the function.
    • Call by Reference: In call by reference, the function receives the address of the variable as an argument (usually using pointers). The function can modify the original value by dereferencing the pointer.
  2. Effect on Original Values:
    • Call by Value: Modifications made to the parameter inside the function do not affect the original value outside the function. The function operates on a copy of the value.
    • Call by Reference: Modifications made to the parameter inside the function directly affect the original value outside the function. The function operates on the actual variable.
  3. Memory Usage:
    • Call by Value: A copy of the value is created and passed to the function, which requires additional memory.
    • Call by Reference: Passing by reference does not create a copy of the value. It directly operates on the original variable, so it does not require extra memory.
  4. Efficiency:
    • Call by Value: Since a copy of the value is passed, it can be less efficient for large data structures or objects.
    • Call by Reference: Passing by reference can be more efficient, especially for large data structures or objects, as it avoids unnecessary copying.
  5. Scope:
    • Call by Value: The function receives a local copy of the value, so any changes made inside the function are limited to the function’s scope.
    • Call by Reference: Since the function operates on the original variable directly, changes made inside the function affect the variable’s scope outside the function.

It’s worth noting that in C, the default method of passing arguments is call by value. To achieve call by reference, you need to use pointers. By understanding these differences, you can choose the appropriate method based on your needs in terms of memory usage, efficiency, and the desired effect on the original values.