Call By Value and Call By Reference in C

While calling the Function in C Programming, we can pass the function parameters in two ways:

  • Call By Value
  • Call By Reference

This article will explain to you the difference between Call By Value and Call By Reference in C Programming language with example.

Call By Value in C

The call by Value in C programming is the safest way to call the functions. In this method, Values of the declared variables passed as the parameters to the function. When we pass them to the function,

  • The compiler is actually making a clone or copy of the original and then passing that clone to the function instead of original values.
  • So, Any changes made to the variables inside a function does not reflect the original values.

Let us look at one example to understand the C call by Value better

Pass parameters to function using Call By Value in C Programming

This program allows the user to enter 2 integers. Next, we pass those numbers to another function to do some calculations using the C Call by value method. Please refer to Functions in C Programming.

/* Call By Value in C Example */

#include <stdio.h>

// Function Declaration
void CallByValue(int , int );          

int main()
{
  int a, b;

  printf ("\nPlease Enter 2 Integer Values\n");
  scanf("%d %d", &a, &b);
  
  printf("\nBefore Calling CallByValue() Function A = %d and B = %d", a, b);
  
  CallByValue(a, b);   

  printf(" \nAfter Calling CallByValue From Main() A = %d and B = %d", a, b);                      
}

void CallByValue(int a, int b)  
{
  a = a * 10;
  b = b * 20;
 
  printf(" \nFrom the CallByValue() Function A = %d and B = %d", a, b);    
}
Call By Value and Call By Reference in C 1

Within this C call by value example, the void CallByValue(int , int ) code is called a function declaration. If you forget this function declaration, then the compiler will throw an error. The printf statement will ask the user to enter 2 numbers. The next statement will store the user input values in a, b variables

Next statement will print the user inputs a and b to compare them later

printf("\n Before Calling CallByValue() Function A = %d and B = %d", a, b);

In the next line, we called the method

CallByValue(5 , 9 );

When the compiler reaches this function, it will traverse to the top to check for the function definition or else declaration of the CallByValue (). If it fails to identify the CallByValue name, then it will throw an error.

In this case, while traversing upwards, it will stop at

void CallByValue(int , int );

because it matches with the one we called

CallByValue(5 , 9 );

The above declaration will take the compiler to the below method.

void CallByValue(int a, int b)

Within the function, We altered the numbers of a and b by multiplying a with 10 and b with 20.

CallByValue(int a, int b)

 a = a * 10;

 b = b * 20;

Which means, CallByValue(5, 9) will become

a = 5 * 10 = 50 and

b = 9 * 20 = 180

Below printf statements is used to print a and b

printf(" \nFrom the CallByValue() Function A = %d and B = %d", a, b); = 50, 180

End of CallByValue()

Lets us see our main()again

printf(" \nAfter Calling CallByValue From Main() A = %d and B = %d", a, b);

It will print a = 5 and b = 9

Although we changed the a and b numbers in the CallByValue (), it will not reflect the originals because CallByValue () used the copy of original values.

Call By Reference in C

In this C call by reference method, the Address of the declared variables passed as the parameters to the function. When we pass the address of the variable to the function,

  • The function is actually accessing the address of the original value (Referring to the address).
  • So, Any changes made to the variables inside function means we are actually changing the original values.

Please be careful while working with this method. If any calculations depend upon the original values, then you may end up with wrong values.

Passing parameters to function using Call By Reference in C

This program allows the user to enter 2 integer values. Then we pass them to swap function to swap those 2 integers using C Call by the reference method.

/* Call By Reference in C Example */

#include <stdio.h>

// Function Declaration
void Swap(int *, int *);          

int main()
{
  int a, b;

  printf ("Please Enter 2 Integers");
  scanf("%d %d", &a, &b);

  printf("\nBefore Swap  A= %d and B= %d", a, b);

  Swap(&a, &b);   

  printf(" \nAfter Swapping From Main() A= %d and B= %d", a, b);                      
}

void Swap(int *x, int *y)
{ 
  int Temp;
  Temp = *x;
  *x = *y;
  *y = Temp;

  /*
  
  *x = 20;
  *y = 40;

  */

  printf(" \nAfter Swapping From UDF A= %d and B= %d", *x, *y);
}
Please Enter 2 Integers
10
25

Before Swap  A= 10 and B= 25 
After Swapping From UDF A= 25 and B= 10 
After Swapping From Main() A= 25 and B= 10

Within this C call by reference program, also called a function declaration. If you forget this declaration, the compiler will throw an error.

void Swap(int , int );

The below statement will ask the user to enter 2 numbers.

printf (" \n Please Enter 2 Integer Values \n ");

The next statement will store the user input values in a, b variables

scanf("%d %d", &a, &b);

The below statement will print the user input values a and b to compare them later

printf("\n Before Swap A = %d and B = %d", a, b);

Let us assume that, the value of a stored at the memory location of 1234 and b stored at the memory location of 6789

In the next line, we called the swap

Swap(&a, &b);

Here &a means the address of a not the value inside the a. It means,

Swap(1234 , 6789 );

Within the function, We declared one more local variable Temp to swap a and b

Temp = *x;

*x = *y;

*y = Temp;

*x is the value inside the address of x.

Which means, the value inside 1234 = 10, and inside 6789 = 25

Swap (10, 25)

The below printf statements is used to print the values of a and b

printf(" \n After Swapping From UDF A = %d and B = %d", *x, *y);

End of Swap()

Lets us see our main() again

printf(" \n After Swapping From Main() A = %d and B = %d", a, b);

It will print a = 25 and b = 10

Here, we changed the original values using the address of them in the Swap function. That’s why this printf statement is showing the output like this.