.

CALL BY REFERENCE AND CALL BY VALUE

Thursday, 15 January 2015
Calling function in a programming is a normal case and in c and c++ it is an art by which one can conquer the heart of these languages.
There are two types of calling which can be done in c and c++. Call by value and call by reference.
In call by value- simple a value is passed to the other method and changes made in that value will not be reflected back in calling function.

In call by  reference - simple a value is passed to the other method and changes made in that value will be reflected back in calling function.

Int a;                                                                      call by value                                                        call by reference
                                                                     
                a                                                              a                              p                                                              a ,p
Calling -method2(a);                                       method3(a)       
Receiving –method2(int p)                          method3(int &p)
Meaning – int p =a;                                         int p=a; but p memory
                                                                                Is same for both.
Void method1()
{
Int  a ;
Cout<<enter the value of a”:
Cin>>a;
//Value  is passed to method2 by call by value function
method2(a);  
//Value  is passed to method3 by call by reference function
Method3(a);
}
/* value is received in method2 and it will be received like p=a; any changes occurring in p cannot be reflected in a  because both are operating on different blocks*/
void method2(int p)
{
//change in p does not mean change in a
P=p+1;
Cout<<p;
}

/* value is received in method3 and it will received like p=a; any changes occurring in p will be reflected in a  because both are operating on same same block */

Void method3(int &p)
{
// change in p means change in a
P=p+1;
Cout<<p;


}

.

Comment here