Home >>C Tutorial >C call By Value and Reference
In order to pass the data into a function in the C language there are two methods that are call by value and call by reference.
In this tutorial we will be discussing these two methods in brief in order to understand them:
Here is an example of the call by value in the C language to clear the basic concept:
#include<stdio.h> void change(int num) { printf("Beforehand the value addition inside function num=%d \n",num); num=num+100; printf("After the value is adeded inside function num=%d \n", num); } int main() { int x=100; printf("Beforehand the function call x=%d \n", x); change(x);//passing value in function printf("After the function call x=%d \n", x); return 0; }
Here is another example(Swap value of two variables)
#include <stdio.h> void swap(int , int); int main() { int a = 10; int b = 20; printf("Beforehand the values are swapped in main a = %d, b = %d\n",a,b); // printing the value of a and b in main swap(a,b); printf("Afterhand the values are swapped in main a = %d, b = %d\n",a,b); // The value of actual parameters do not change by changing the formal parameters in call by value, a = 10, b = 20 } void swap (int a, int b) { int temp; temp = a; a=b; b=temp; printf("After the values are swapped in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b = 10 }
Here is an example of call by reference:
#include<stdio.h> void change(int *num) { printf("Before the values are added inside function num=%d \n",*num); (*num) += 100; printf("After the values are added inside function num=%d \n", *num); } int main() { int x=100; printf("Before the function call x=%d \n", x); change(&x);//passing reference in function printf("After the function call x=%d \n", x); return 0; }
Here is another example of the call by reference method in C:
#include <stdio.h> void swap(int *, int *); //prototype of the function int main() { int a = 10; int b = 20; printf("Before the values are swapped in main a = %d, b = %d\n",a,b); // printing the value of a and b in main swap(&a,&b); printf("After the values are swapped in main a = %d, b = %d\n",a,b); // The values of actual parameters do change in call by reference, a = 10, b = 20 } void swap (int *a, int *b) { int temp; temp = *a; *a=*b; *b=temp; printf("After the values are swapped in function a = %d, b = %d\n",*a,*b); // Formal parameters, a = 20, b = 10 }
The C language is known to for its feasibility and functions. Now the functions needed to be called in order to execute them and they can be invoked in these two ways i.e. Call by Value or Call by Reference in C language. These are the two ways that are generally known to be differentiated by the type of values passed by them as their parameters. There’s a catch in understanding parameters, actually the parameters that are passed to function are called actual parameters and the parameters that are received by function are called formal parameters.
Call by value | Call by reference |
---|---|
In this method, a copy of the value is passed into the function. | In this method, an address of the value is passed into the function |
The changes that are made inside the function is only limited to the function. By changing the formal parameters the values of the actual parameters does not change. | The changes that are made inside the function also validate outside of the function. By changing the formal parameters the values of the actual parameters does change. |
The memory location is different where the actual and formal arguments are created. | The memory location is the same where the actual and formal arguments are created. |