HowToProgramC : Lesson 12


Back Home Next



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:

#include <iostream.h>
void f(int); //Prototype of the function
void main()
{
int i;
i = 10;
cout << “\n” << ” In main(), the value of i is: “ << i;
f(i);
cout <<“\n” << ” Back in main(), the value of i is: “ << i;
}
void f (int i)
{
i *= 2;
cout << “\n” << “ In f(), the value of i is: “ << i;
}

The output of this program is as under:
In main(), the value of i is: 10
In f(), the value of i is: 20
Back in main(), the value of i is: 10

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
get the address of a variable. Let's look at a function, which actually is a modification of our previous square() function.

#include <iostream.h>
void square(double);
void main()
{
double x;
x = 123.456;
cout << “\n” << “ In main(), before calling square(), x = “ << x;
square(&x); //Passing address of the variable x
cout << “\n” << “ In main(), after calling square(), x = “ << x;
}
void square(double*x)//read as:x is a pointer of type double
{
*x = *x * *x; //Notice that there is no space in *x
}

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
number to the function square(double*) and asked it to take the value inside that box, multiply it with itself and put the result back in the same box. This is the mechanism of ‘Call by Reference’.

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.
The output of the program will be as under:

In main(), before calling square(), x = 123.456
In main(), after calling square(), x = 15241.4

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.



Back Home Next


Google




learntoknow@yahoo.com
© All rights Reserved.