Reference Variables


A reference variable allows us to create an alternative name for already defined variable.
It is introduced in C++.
Once you define a reference variable that references already defined variable then you can use any of them alternatively in the program. Both refer to the same memory location.
Thus if you change value of any of the variable it will affect both variables because one variable reference to another variable.
The general syntax of creating reference variable is given below: Data-type & Referace_Name = Variable_Name;
The reference variable must initialize at the time of declaration.

     

Example:
int count;
count = 5;
Int & incre = count;
Here,
We have already defined variable named count.
Then we create a reference variable named incre that refer to the variable count.

Referance Variable

Since both variable refer to same memory location if you change value of variable count using following statement:
Count=count + 5;
Will change the value of variable count and incre to 10.
The major use of reference variable is in function. If we want to pass reference of the variable at the time of function call so that the function works with original variable we can use reference variable.

Download Projects


Download Programs