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 the values to the function,
- The compiler is actually making a clone or copy of the original value and then passing that clone values to the function instead of original values.
- So, Any changes made to the values of 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 integer values. Next, we pass those values 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); }
void CallByValue(int , int );
Within this C call by value example, the above code is called a function declaration. If you forget this function declaration, then the compiler will throw an error.
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);
Next statement will print the user input values 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 function
CallByValue(5 , 9 );
When the compiler reaches this function, it will traverse to the top to check for the function definition or else function declaration of the CallByValue () function. If it fails to identify the function with 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 function we called
CallByValue(5 , 9 );
The above function declaration will take the compiler to the below function.
void CallByValue(int a, int b)
Function Definition
Within the function, We altered the values 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 the values of a and b
printf(" \nFrom the CallByValue() Function A = %d and B = %d", a, b); = 50, 180
End of CallByValue() function
Lets us see our main() function 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 values in the CallByValue () function it will not reflect the original values because CallByValue () function 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 values of 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 those values 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 Integer Values"); 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); }
Within this C call by reference program, also called a function declaration. If you forget this function 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 value of b stored at the memory location of 6789
In the next line, we called the function
Swap(&a, &b);
Here &a means the address of a not the value inside the a. It means,
Swap(1234 , 6789 );
Function Definition
Within the function, We declared one more local variable Temp to swap the values of 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 value 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() function
Lets us see our main() function 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.