|Lessons||Lesson 12: Function Calling|
1. Function Calling by Value
2. Fuction Calling by Reference.
In the following text we explain both of the function calling mechanisms of C / C++ in some detail.
Function Calling (Call by Value):The default function calling mechanism of C is a 'Call by Value'. It means that when we call a function and pass some arguments (variables) to it, we are passing a copy of the arguments (variables) instead of original variables. The copy reaches to the function that uses it in whatever way it wants and returns it back to the calling function. The passed copy of the variable is used and
original variable is not touched. The default of C is 'Call by Value'. It is better to use it as it saves us from unwanted side effects. Relatively, 'Call by Reference' is a bit complex but it may be required sometimes when we want the actual variable to be changed by the function being called.
Let's consider an example to comprehend 'Call by Value' and how it works. Suppose we write a main() function and another small function f(int) to call it from main(). This function f( ) accepts an integer, doubles it and returns it back to the main() function. Our program would look like this:
The output of this program is as under:
As the output shows the value of the variable ‘i’ inside function main() did not change, it proves the point that the call was made by value.
Function Calling (Call by Reference):
We would like to use 'call by reference' while using a function to change
the value of the original variable. Let's consider the square(double)
function again, this time we want the original variable ‘x’
to be squared. For this purpose, we passed a variable to the square()
function and as a result, on the contrary to the ‘Call by Value’,
it affected the calling functions original variable. So these kinds of
functions are ‘Call by Reference’ functions. Let us see, what
actually happens inside Call by Reference? As apparent from the name ‘By
Reference’, we are not passing the value itself but someform of
reference or address. To understand this, you can think in terms of variables
which are names of memory locations. We always access a variable by its
name (which in fact is accessing a memory location), a variable name acts
as an address of the memory location of the variable.If we want the called
function to change the value of a variable of the calling function, we
must pass the address of that variable to the called function. Thus, by
passing the address of the variable to the called function, we convey
to the function that the number you should change is lying inside this
passed memory location, square it and put the result again inside that
memory location. When the calling function gets the control back after
calling the called function, it gets the changed value back in the same
memory location. In summary, while using the call by reference method,
we can’t pass the value. We have to pass the memory address of the
value. This introduces a new mechanism which is achieved by using ‘&’
(ampersand) operator in C language. This ‘&’ operator
is used to
Here *x means whatever the x points to and &x means address of the
variable x. We will discuss Pointers in detail later. We are calling function
square(double*) with the statement square(&x) that is actually passing
the address of the variable x , not its value. In other words, we have
told a box
Notice that there is no return statement of square(double*) as we are
putting the changed value (that could be returned) inside the same memory
location that was passed by the calling function.
In main(), before
calling square(), x = 123.456
By and large, we try to avoid a call by reference. Why? Mainly due to the side-effects, its use may cause. As mentioned above, it will be risky to tell the address of some variables to the called function. Also, see the code above for some special arrangements for call by reference in C language. Only when extremely needed, like the size of the data to be passed as value is huge or original variable is required to be changed, you should go for call by reference, otherwise stick to the call by value convention.
NEXT>>>>>Lesson 13. Recursive Functions