Call by Value & Call by Reference in C

Subject: PPS (Programming for Problem Solving)

Contributed By: Sanjay

Created At: February 3, 2025

Question:


List and explain the types of parameter passing techniques in functions with example

Explanation Video:

Custom Image

Explanation:

 

Types of Parameter Passing Techniques in Functions

In C, function parameters can be passed to functions in two ways:

  1. Call by Value (Pass by Value)

  2. Call by Reference (Pass by Address or Pointer)


1. Call by Value (Pass by Value)

  • A copy of the actual parameter is passed to the function.
  • Any changes made inside the function do not affect the original value in the calling function.
  • It is safe since the original variable remains unchanged.

Example of Call by Value:

#include <stdio.h>

void changeValue(int x) { 

   x = 100;  // Change the value of x

   printf("Inside function: x = %d\n", x);

}

int main() {

   int num = 50;

   printf("Before function call: num = %d\n", num);

   changeValue(num);  // Function call (pass by value)

   printf("After function call: num = %d\n", num);

   return 0;

}

Output:

Before function call: num = 50 

Inside function: x = 100 

After function call: num = 50 

Explanation:

  • The function changeValue() receives a copy of num.
  • Inside the function, the value changes to 100, but this change does not reflect in main().
  • This proves that the original variable remains unchanged.

2. Call by Reference (Pass by Address or Pointer)

  • Instead of passing a copy, the actual memory address (reference) of the variable is passed.
  • Changes made inside the function directly modify the original variable.
  • This is useful for modifying values and working with large data structures (e.g., arrays).

Example of Call by Reference:

#include <stdio.h>

 

void changeValue(int *x) { 

   *x = 100;  // Modify the value at the memory address

   printf("Inside function: *x = %d\n", *x);

}

int main() {

   int num = 50;

   printf("Before function call: num = %d\n", num);

   changeValue(&num);  // Function call (pass by reference)

   printf("After function call: num = %d\n", num);

   return 0;

}

Output:

Before function call: num = 50 

Inside function: *x = 100 

After function call: num = 100 

Explanation:

  • The function changeValue() receives the address of num using a pointer (int *x).
  • Modifying *x directly affects the original num in main().
  • The value is changed permanently because we are working with the actual memory location.

Key Differences Between Call by Value and Call by Reference

Feature

Call by Value

Call by Reference

Modification

Does not modify original value

Modifies original value

Memory Usage

More (creates a copy)

Less (uses same memory)

Data Safety

Safe (no accidental modification)

Risky (modifies actual data)

Use Cases

When original data should not be changed

When modifying data is necessary


  • Call by Value is used when we do not want the function to modify the original data.
  • Call by Reference is used when we want the function to modify the original variable.
  • In C, arrays are always passed by reference because their base address is passed automatically.
Share this Article & Support Us:
Status
printf('Loading...');