Wednesday, February 9, 2011

call by value and call by reference c++

Are you looking for call by value and call by reference in c++? Do you want to understand the working of call by value and call by reference in c++? Do you want to understand the concepts of call by value and call by reference in c++? If yes, read ahead.

What is call by value and call by reference in c++?

Call by value and call by reference are two mechanisms in c++ to invoke functions with parameters as value or reference. Both the concepts have a different working.

What is difference between call by value and call by reference in c++? How call by value and call by reference work in c++?

In call by value mechanism, parameters are passed by value. It means that the called function makes a copy of parameters and these copied values have nothing to do with original values of parameters sent by calling function. If you make changes in the values of parameters of called function, the changes will not be reflected back in the values of parameters of calling function because the called and calling function have different copies of variables at different memory locations

In call by reference mechanism, parameters are passed by reference. It means that the called function does not make a copy of parameters sent by calling function rather called function takes the original address of parameters sent by calling function. If you make changes in the values of parameters of called function, the change will be reflected back in the values of parameters of calling function because the called and calling function share the parameters at same memory locations.

See the working of call by value and call by reference in c++ by following programs :

Program to display Call by value in C++

#include<iostream.h>
void swap(int,int);
void main()
{
int a=5,b=7;
cout<<"Value of a and b in main function before swap:"<<a<<b;
swap(a,b);
cout<<"Value of a and b in main function after swap:"<<a<<b;
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"Value of a and b in swap function:"<<a<<b;
}

PROGRAM OUTPUT:

Value of a and b in main function before swap:5 7
Value of a and b in swap function:7 5
Value of a and b in main function after swap:5 7

Program Explanation:
Line 1: including header file in the program
Line 2: swap function prototype
Line 5: 2 integer variables a and b declared and initialized with values of 5 and 7 respectively
Line 7: Call to function swap with parameters a and b
Line 10-17: swap function swapping the values of a and b.

Program to display Call by reference in C++
#include
void swap(int&,int&);
void main()
{
int a=5,b=7;
cout<<"Value of a and b in main function before swap:"<<a<<b;
swap(a,b);
cout<<"Value of a and b in main function after swap:"<<a<<b;
}
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"Value of a and b in swap function:"<<a<<b;
}
PROGRAM OUTPUT:
Value of a and b in main function before swap:5 7
Value of a and b in swap function:7 5
Value of a and b in main function after swap:7 5
Program Explanation:
Line 1: including header file in the program
Line 2: swap function prototype
Line 5: 2 integer variables a and b declared and initialized with values of 5 and 7 respectively
Line 7: Call to function swap with parameters a and b
Line 10-17: swap function taking parameters by reference.