Explain pass by value and pass by reference in C programing

(Last Updated On: February 25, 2019)

Answer: Detailed explanation of pass by value and pass by reference in C programing 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, there is no pass by reference, but, still we use this terminology in C programming 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 value inside a function it will not change original one resides within the calling routine. 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 routine.

C program example for pass by value and pass by reference with comments:




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.



It is correct that a function can return only one value. If we need to return multiple values from a function, we use call by reference in C.