Difference Between Call by Value and Call by Reference in C

Get Certified in C Programming for Free and Take Your Skills to the Next Level

Introduction to Function Parameter Passing

In C programming, there are two main ways to pass parameters to functions – call by value and call by reference. Understanding how these parameter-passing mechanisms work is crucial for writing efficient C code.

When a function is invoked in C, the values provided as arguments are known as actual parameters. The variables defined in the function declaration that accept these values are called formal parameters. The relationship between actual and formal parameters determines how C handles parameter passing.

Call by Value in C

In C, calling a function by value is the default way of giving parameters. The values of the actual parameters are replicated into the formal parameters when call by value is used.

Here is a simple example:

#include <stdio.h>

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

int main() {

  int x = 99;
  int y = 199;

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

  swap(x, y);

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

Output:
Before swap: x = 199, y = 99
After swap: x = 99, y = 99

In this program, x and y are passed to the swap() function using call by value. The swap() function receives copies of x and y in the formal parameters a and b. Swapping the values of a and b has no effect on the actual parameters x and y in main().

Key points about the call by value:

  • Formal parameters get copies of the actual values allocated in different memory locations.
  • Any changes made to formal parameters do not affect the actual parameters.

Call by Reference in C

In call by reference, instead of passing copies of values, the addresses of the actual parameters are passed to the formal parameters. This allows both the actual and formal parameters to refer to the same memory locations.

Here is an example of call by reference in C using pointers:

#include <stdio.h>

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

int main() {

  int x = 199;
  int y = 99;

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

  swap(&x, &y);  

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

Output:
Before swap: x = 199, y = 99
After swap: x = 99, y = 199

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Here, the addresses of x and y are passed to pointers a and b respectively. Now, both x, y and a, b refer to the same locations, so swapping in the function also swaps the values in main().

Key points about call by reference:

  • Addresses of actual parameters are passed to formal parameters.
  • The same memory locations are referred to by both the formal and real parameters.
  • Changes made in the function are reflected back in the calling function.

Differences between Call by Value and Call by Reference in C

Basis of ComparisonCall By ValueCall By Reference
Parameter passingThe values of actual parameters are copied to formal parametersThe addresses of actual parameters are passed to formal parameters
Access to actual parametersFormal parameters get a copy of the value, allocated separately in memory. There is no access to actual parameters.Actual parameters and formal parameters both relate to the same memory address. It is possible to change the real settings.
Changes to formal parametersChanges made to formal parameters do not impact actual parametersChanges made to formal parameters modify the actual parameters
Necessary syntaxSimple passing of values to parametersPointer syntax needed to pass addresses to parameters
Use casesWhen a function needs to work on a copy of data without modifying the originalWhen a function needs to modify the actual arguments passed to it
Performance and memoryAdditional memory is needed to make copies. Less efficient.More efficient as no copying is required. But risks inadvertent changes.
Data amountBest for passing small amounts of dataUseful when passing large amounts of data
Variable manipulationCannot manipulate actual variablesCan directly manipulate actual variables
Preferred usageWhen actual parameters should not be changedWhen a function needs to change actual parameters

Differences between Call by Value and Call by Reference

Examples to Illustrate Call by Value and Call by Reference in C

Here are some additional examples to demonstrate call by value and call by reference:

Call by Value

void cubeByVal(int n) {
  n = n * n * n; 
}

int main() {
  int num = 5;
  cubeByVal(num);
  printf("Number is %d", num); // 5  
}

Output:
Number is 5

Since a copy of num is passed, the value remains unchanged.

Call by Reference

void cubeByRef(int *n) {
  *n = *n * *n * *n;
}

int main() {
  int num = 5;
  cubeByRef(&num);
  printf("Number is %d", num); // 125
}

Output:
Number is 125

Here num is passed by reference and modified.

Use Call by Value and Call by Reference in C

So when should you use call by value vs call by reference in C?

Use call by value when:

  • You want to avoid the side effects of modifying actual parameters.
  • Function needs to work on a copy of data without changing the original.

Use call by reference when:

  • Function needs to modify the actual arguments.
  • Need higher performance by avoiding copying data.
  • Don’t mind side effects on actual parameters.

Call by reference has performance benefits as data is not copied. But it can cause inadvertent changes to variables in the calling code.

Advantages and Disadvantages of Call by Value and Call by Reference in C

There are certain tradeoffs to consider when choosing between call by value and call by reference in C functions.

Let’s examine the pros and cons of each method:

Advantages of Call by Value

  • Isolation: The function cannot modify the actual arguments passed to it. This avoids inadvertent side effects on variables in the calling function.
  • Data protection: The function works on a separate copy of the data rather than directly accessing the original data. This is safer.
  • Reusability: As the function cannot modify original data, it is less likely to cause issues when reused with other parameters.
  • Testing: call by value allows easy testing functions with different arguments since no changes are persistent.

Disadvantages of Call by Value

  • Memory: Additional memory is required to make copies of arguments for the function parameters.
  • Performance: Copying arguments takes processing time and reduces efficiency, especially for large data structures.
  • No modification: Since copies are passed, any changes made inside the function are not reflected in the calling function.

Advantages of Call by Reference

  • Efficiency: No copying of arguments takes place since pointers to the original data are used. This improves performance.
  • Modification: The function can modify the actual arguments passed to it. Any changes made are reflected back.
  • Memory: No extra memory is needed as no copying takes place. More efficient for large data structures.

Disadvantages of Call by Reference

  • No isolation: Lack of isolation means the function can modify the actual parameters unexpectedly.
  • Side effects: Changes made to actual parameters in the function lead to side effects in the calling code.
  • Pointer handling: Pointers can be mishandled, leading to unexpected results and errors. Needs more care.
  • Testing: Repeated testing is harder since calls modify data persistently.

Conclusion

Understanding parameter passing mechanisms is vital for writing good C programs. Call by value and call by reference in C allows passing data to functions in different ways.

Call by value maintains isolation while call by reference allows modifying actual arguments. Knowing their precise differences in terms of memory, impact, and performance will help them choose the right technique and write better C code.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *