In C++ you can use both pass by value and pass by reference.In C++There are two types of pass by reference.1)Pass by pointers
2)Pass by reference
pointer types store the address of particular type. Reference is just another name for that type .Once you assign a value to reference , you can not reassign it.Unlike references you can reassign values to pointers.
int a = 12;
int *b = &a; //this is a integer type pointer that stores the address of a
int &c =a; //this integer type reference
i think previous answers tell difference between pass by value and pass by reference.In pass by value , you pass an exact copy of values.Performance wise this is not good.In pass by reference ,you do not pass copy values or objects, just pass address or reference .
int myFuntion(int a,int b); //funtion prototype
Pass by value
int x=2;
int y =3;
myFuntion(x,y);
pass by reference using pointers
int x=2;
int y =3;
int myFuntion(int *a, int *b);
myFuntion(&x,&y);
Pass by reference by references
int myFuntion(int &a, int &b);
int x=2;
int y =3;
myFuntion(x,y);
There is an another use for pass by reference is that you can modify original variables .In pass by value you can not do this.
void swap (int& first, int& second)
{
int temp = first;
first = second;
second = temp;
}
int a = 2;
int b = 3;
swap( a, b );
Some times you do not need to modify originals .Just pass as const references or pointers
myFuntion(const int &z);
Diffidence between pass by pointers vs Pass by references
pointers are more prone to bugs. so if possible use references .Use pointers when needed.
reference can not take null values directly. when your function need to take null values use pointers.
Other than this , pass by references is a good way of returning multiple values .Normally a function can return a single value.But using pass by references you can return more than one values.