Friday, 3 May 2019

Learn the Concept of Call by Reference in C Programming Language


Call by reference is one of a couple of methods meant for passing the data within the function in C language. It is here to mention that there are a couple of methods, called the ‘call by value and call by reference’ available to pass the data within the function in C.

In call by reference, the variable’s address is put inside the function call as the actual parameter. Actual parameter’s value can be changed upon making changes in the basic parameters after the address of the actual parameters is put. 



The way of allocating memory is quite identical for formal parameters, as well as the actual parameters in case of call by reference. The entire actions in the functions are done as per the value kept at the address of the actual parameters; also at the same address, the changed value gets stored.

What does it mean by function call by reference?

Upon a function being called by putting the address of actual parameters, it is the method of calling the function, called as the call by reference. In case of call by reference, the actions taken are all the formal parameter, which affects the actual parameter’s value as the entire job is done on the value stored within the actual parameter’s address. 

In case of call by reference, the method of putting arguments within a function is about copying the address of an argument within the formal parameter. Within the function, the address is made use of for accessing the actual argument that is used in the concerned call. This tells that the changes made to the parameter affect the argument that is passed in.

In case of call by reference value passing, an argument pointer is delivered within the functions very much as of any other conventional value. In accordance, the coder needs to provide the function parameters as pointer types that make the exchange of the values of both the integer variables pointed to, through their arguments.

Difference between call by value and call by reference

It is important to understand the difference between call by value and call by reference as most people confuse while distinguishing between the two. In case of call by value, a copy of the value is delivered within the function. In case of call by reference, the address of value is put into the function.

When it comes about the call by value, the changes made within the function is confined within the function only. Here the values of the actual parameters don’t alter upon altering the formal parameters. On the other hand, in case of call by reference, changes made within the function are validated at the exterior of the functions as well. Here the values of the actual parameters do alter upon changing the formal parameters.

In case of call by value, the actual and formal arguments are developed at distinct memory locations. When it comes about the call by reference, both the actual and formal arguments are made within the same memory location.