METHODS OF CALLING FUNCTIONS IN C++

A function can be invoked in two manners:

(i) CALL BY VALUE    (ii) CALL BY REFERENCE


CALL BY VALUE


The call by value method copies the values of actual parameters into the formal parameters, that is, the function creates its own copy of argument values and then uses them. 

Thus, in call by value method, the changes are not reflected back to the original values.

The main benefit of call by value method is that you cannot alter the variables that are used to call the function because any change that occurs inside function is on the function's copy of the argument value. The original copy of the argument value remains intact(not changed). 

Note:- The format of programs given here works only with TURBO C++ IDE.


PROGRAM TO ILLUSTRATE THE CALL BY VALUE                                

               METHOD OF FUNCTION INVOKING






OUTPUT OF THE PROGRAM CODE:-



In the above program, the value of the argument to change(). orig i.e. 10 is copied onto parameter a so that a gets value 10. When the statement a=20 takes place, the value of a is changed but not the value of orig. The orig has still the value 10.

What occurs inside the function has no effect on the variable used in the function call. 

In call by value method, the called function creates a new set of variables and copies the variables of arguments into them. The function does not have access to the original variables(actual parameters) and can only work on the copies of values it created. Passing arguments by values is useful when the original values are not to be modified. In fact, call by value method offers insurance that the function cannot harm the original value.

CALL BY REFERENCE 

The call by reference method uses a different mechanism. In place of passing a value to the function being called, a reference to the original variable is passed. A reference is a different same for a predefined variable. That is, the same variables value can be accessed by any of the two names: the original variable's name and the reference name.

In call by reference method, the calling function does not create its own copy of original values, rather, it refers to the original values only by different names i.e., the reference. Thus the called function works with the original data and any change in the values gets reflected to the data. 

Thus, in call by reference method, the changes are reflected back to the original values.
The call by reference method is useful in situations where the values of the original variables are to be changed using a function. For instance, a function is to be invoked that swaps the values of two variables.

PROGRAM TO SHOW HANDICAP OF CALL BY VALUE METHOD  





OUTPUT OF THE PROGRAM CODE:-


When the function swap() is invoked, the values of a and b(actual parameters) get copied onto x and y(formal parameters). The swap() does swap the values but x and y values get swapped; and b remain intact(nothing happened to them). Therefore, after swap() is over a is still 7 and b is still 4.

Call by value method is inappropriate for the situations wherein some changes have to be reflected to original values. 

For invoking a function using call by reference, the following points are to be kept in mind:-

The function call statement remains the same for call by reference also i.e., it'll still be:

swap(a,b);             // the function call

Our function declaration will be as follows :

void swap(int &x, int &y);      // the function prototype
That is the function swap creates references for two incoming integers. 
& operator creates a reference. 

The function declarator in the function definition will be:-

void swap(int &x,int &y)           // only declarator changes
{   :
     :       // rest of the function definition remains the same
     :
}

This means the function swap() creates reference x for the first incoming integer and the reference y for the second incoming integer. The original values will be worked with but by using the names x and y.


PROGRAM TO SWAP TWO VALUES




OUTPUT OF THE PROGRAM CODE:-


NOTE:- 
A function call need not be purely by value or by reference. A function can be invoked by using mix of the two methods as shown below in a function declaration:-

void trunk(float,float &,int &)

The above declaration depicts the function trunk() can be invoked by passing the first value(which is float) by call by value method and the rest two values(float and int) by call by reference method. That is, the function trunk() will create its own copy for the first parameter it receive and the references for the rest two parameters.  


DOWNLOAD THE NOTES BY CLICKING THE LINK BELOW

DOWNLOAD (http://sdu.bz/8JePPZ6V)
Previous
Next Post »
Thanks for your comment