C Programs Tutorials | IT Developer
IT Developer

C Programming - C Function - Call by Value



Share with a Friend

C Programming - C Function - Call by Value

C Function - Call by Value

In C, Call by Value refers to the method of passing arguments to a function in which the actual parameter's value is passed to the function. The function works with a copy of the value, so any modifications made to the parameter inside the function do not affect the original argument.

How Call by Value Works

  1. Passing Parameters: In Call by Value, when a function is called, the value of the argument is copied into the formal parameter of the function.
  2. Local Copy: Inside the function, the formal parameter acts as a local variable, which holds a copy of the actual value passed.
  3. No Effect on Actual Argument: Changes made to the formal parameter inside the function do not affect the original argument because only a copy is modified.

Syntax of Call by Value

C

return_type function_name(parameter1, parameter2, ...) {

    // Code that uses parameters

    // Changes to parameters won't affect actual arguments

}

Example of Call by Value

C

#include <stdio.h>

// Function Declaration

void swap(int a, int b);

int main() {

    int x = 5, y = 10;

    printf("Before swap: x = %d, y = %d\n", x, y);

    // Function Call

    swap(x, y);

    printf("After swap: x = %d, y = %d\n", x, y);

    return 0;

}

// Function Definition

void swap(int a, int b) {

    int temp;

    temp = a;

    a = b;

    b = temp;

    // This swap does not affect the original variables in main

    printf("Inside swap: a = %d, b = %d\n", a, b);

}

Explanation

  • In the main() function, two integers x and y are defined with values 5 and 10, respectively.
  • The swap() function is called with x and y passed as arguments.
  • Inside the swap() function, the values of a and b are swapped.
  • However, since the swap() function uses Call by Value, the original values of x and y in main() remain unchanged.

Output:

Before swap: x = 5, y = 10

Inside swap: a = 10, b = 5

After swap: x = 5, y = 10

Explanation of Output:

  • Inside the swap() function, a and b are swapped, but these changes only affect the local copies a and b. The original values x and y in main() are not changed.
  • This is a clear example of Call by Value.

Key Points of Call by Value

  • Modifications inside the function do not affect the actual arguments passed from the calling function.
  • A copy of the actual parameter is passed to the function.
  • Useful when you do not want to alter the original values in the calling function.
  • The original variable's value remains unchanged outside the function.

Advantages of Call by Value

  1. Safety: The original arguments are not modified, providing safety from unintentional changes.
  2. Simplicity: It is easier to understand and implement as only a copy of the argument is passed.

Disadvantages of Call by Value

  1. Memory Overhead: A copy of the arguments is created, which can consume extra memory, especially for large data structures (like arrays or structs).
  2. Limited Modification: Since the actual values are not modified, any changes to the parameters inside the function do not affect the calling function.

When to Use Call by Value

  • When you need to protect the original data from being modified.
  • When the arguments are small in size and do not require modification. For larger data, a different method like Call by Reference is often more efficient.