Reference variable in C++

in brief, when a variable is declared as reference(& variable) then it will become an alternative name for the current variable

We can also say, it is a linked variable because a change in one variable makes the changes in another variable

Note: in C we say (&) as an address operator but in C++ we  refer to it as a reference variable

But, Where do we use it?

  • Copy constructor
  • Operator overloading

Initialization of reference variable

A reference variable must be initialized when it created

int a=8;
int&b; //error
b=a;

Example:

Float account_number=255; // here account_number is a variable name
Float &ac_num= account_number; // here ac_num is a reference name

If we create the variable ac_num as a reference to the variable account_number then account_number and ac_num can be used interchangeably to represent the same variable

Both account_number and ac_num will refer to the same variable

#include <iostream>
using namespace std;
int main()
{
    int a=10;// here a is a variable name
    int &b=a;//b is a reference name to a variable a
    int &c=b;//c is a reference name to a variable b
    cout <<"a=" <<a<<endl;
    cout <<"b=" <<b<<endl;
    cout <<"c=" <<c<<endl;
    c=900;//Change in one variable makes the change in the remaining variables which is associated with it
    cout <<"a=" <<a<<endl;
    cout <<"b=" <<b<<endl;
    cout <<"c=" <<c<<endl;
    return 0;
}

Output

a=10
b=10
c=10
a=900
b=900
c=900

not to mention, in C we can pass the parameters to the functions in two ways

  • Call by value
  • Call by address

In C++ we can pass the parameters to the functions by an additionally  introduced concept called call by reference

The problem:

In C when we make a function call by address we need to declare formal parameters as pointers

  • Pointers are not secure
  • Reference variables are secured when compared with pointer variables
  • Referencing offers a clean elegant and efficient way to pass parameters to functions

Finally, instead of passing parameters to functions by call by value or call by address, we can use call by reference

Call by value

#include <iostream>
using namespace std;
void swap_value(int a ,int b );
int main()
{
    int a=100,b=200;
    swap_value(a,b);// call by value
    return 0;
}
void swap_value(int a,int b)
{
    int temp;
    temp= a;
    a=b;
    b=temp;
    cout <<"call by value" <<endl;
     cout <<"a=" <<a<<endl;
    cout <<"b=" <<b<<endl;
}

Output

call by value
a=200
b=100

Call by address

#include <iostream>
using namespace std;
void swap_address(int *a,int*b);

int main()
{
    int a=100,b=200;
      swap_address(&a,&b);// call by address
    cout <<"a=" <<a<<endl;
    cout <<"b=" <<b<<endl;
    return 0;
}
void swap_address(int *a,int*b)
{
    int temp;
    temp= *a;
    *a=*b;
    *b=temp;
    cout <<"call by address" <<endl;
}

Output

call by address
a=200
b=100

Call by reference

#include <iostream>
using namespace std;
void swap_reference(int &a,int &b);
int main()
{
    int a=100,b=200;
    swap_reference(a,b);// call by reference
    cout <<"a=" <<a<<endl;
    cout <<"b=" <<b<<endl;
    return 0;
}
void swap_reference(int &a,int &b)
{
    int temp;
    temp= a;
    a=b;
    b=temp;
    cout <<"call by reference" <<endl;
}

Output

call by reference
a=200
b=100

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.