Answer: Detailed explanation of pass by value and pass by reference in C programming with example. Parameters to a function can be passed in two ways i.e. pass by value and pass by reference in C language programming.
Actually, pass by reference is nothing but the address of variable that we pass to a function as a parameter.
In C programming, there is no pass by reference, but, still we use this terminology in C language also. In fact, pass by reference feature is there in C++.)
Pass by Value in C
When we pass arguments to a function by value, a copy of that value goes to the function parameter. So, now we have two separate values i.e. one original and another one local to the function.
Hence, if we alter the value inside a function, it will not change the original one resides within the calling function. This procedure of passing values as an argument to a function is known as passing by value.
Pass by Reference in C
Passing arguments to a called function by reference, means, passing the address of memory location of the data to the function using &operator.
As, we are passing address of the data to the function, if we change the data on that address inside the function, it will be reflected outside the function too i.e. in calling function.
C program example for pass by value and pass by reference with comments:
//funcByValue : recives copy of data
void funcByValue(int a){
//Since, copy is recieved, it will have different memory location.
printf("Address of value a,inside function funcByValue is: %u\n",&a);
a=2;
}
//func by reference recieves address of data in a pointer.
void funcByReference(int *a){
printf("Address of value a,inside function funcByReference is: %u\n",a);
//changing data at the memory location, so, changes will be reflected outside of this funtion too.
*a=2;
}
//Test call by value and call by reference.
int main(){
int i=1;
//Call by value
//Value of i will not be chaned here even though we have changed
//inside the function funcByValue(),as this function recievs this parameter as
//a copy of data i.
funcByValue(i);
printf("After Call By Value: %d\n",i);
//Call by Reference
//After this call value of i will be changed here in calling routin as
//the value of i will be modified at its address/memory location itself
//inside the funcByReference() method.
//Passing address of varialbe i
printf("Address of value i is: %u\n",&i);
funcByReference(&i);
printf("After call By Reference: %d\n",i);
return 0;
}
Output:
Address of value a,inside function funcByValue is: 3209252
After Call By Value: 1
Address of value i is: 3209464
Address of value a,inside function funcByReference is: 3209464
After call By Reference: 2
Note that in pass by value original varialbe and a copy variable(inside funtion patameter) have differet address.
in pass by reference case, address of variable i in calling routine and inside function is same.
NOTES:
It is correct that a function can return only one value. If we need to return multiple values from a function, we can use call by reference in C.