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 and Call By Reference.

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

Call By Value in C

The call by Value in C programming is the safest way to call the functions. In this method, the Values of the declared variables are 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 the original values.
  • So, Any changes made to the variables inside a function do not reflect the original values.

Let us look at one C programming example to better understand the call by Value and call by reference.

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.

#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 and b variables.

The 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 the 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 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 by 10 and b by 20.

CallByValue(int a, int b)

 a = a * 10;

 b = b * 20;

This means CallByValue(5, 9) will become

a = 5 * 10 = 50 and

b = 9 * 20 = 180

The below printf statement 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 a copy of the original values.

We hope you understood the C Programming Language Call by Value, so let us explore the Call by Reference.

Call By Reference in C

In this C Call By Reference method, the Address of the declared variables is passed as the function’s parameters. 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 the function mean we are actually changing the original values.

Please be careful while working with this C Call By Reference method. If any calculations depend upon the original values, you may have the 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 the C swap function to swap those 2 integers using Call by the reference method.

#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 the a and 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 is 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 are 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 their address of them in the Swap function. That’s why this printf statement shows the output like this.