Site icon DataFlair

Difference Between Call by Value and Call by Reference in C

call by reference in c

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:

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

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:

Differences between Call by Value and Call by Reference in C

Basis of Comparison Call By Value Call By Reference
Parameter passing The values of actual parameters are copied to formal parameters The addresses of actual parameters are passed to formal parameters
Access to actual parameters Formal 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 parameters Changes made to formal parameters do not impact actual parameters Changes made to formal parameters modify the actual parameters
Necessary syntax Simple passing of values to parameters Pointer syntax needed to pass addresses to parameters
Use cases When a function needs to work on a copy of data without modifying the original When a function needs to modify the actual arguments passed to it
Performance and memory Additional memory is needed to make copies. Less efficient. More efficient as no copying is required. But risks inadvertent changes.
Data amount Best for passing small amounts of data Useful when passing large amounts of data
Variable manipulation Cannot manipulate actual variables Can directly manipulate actual variables
Preferred usage When actual parameters should not be changed When a function needs to change actual parameters

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:

Use call by reference when:

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

Disadvantages of Call by Value

Advantages of Call by Reference

Disadvantages of Call by Reference

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.

Exit mobile version