Skip to main content

C++ Pass by Reference and Value Beginner Programming

Animated Gif of Pass by Reference & Value

C++ pass by Reference & value

There is two way of accessing values of variables and passing values in function argument, first one is pass by value and second one is pass by reference.


Reference variable is another name for a existing variable. Once a reference variable is initialized the reference name may be used to refer to the variable.
A reference must be initialized when it is created.You can't have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.Once a reference is initialized to an object, it cannot be changed to refer to another object.
& operator use to declare reference variable which is use to store variable value, you can access the value of the variable by using original variable name or by using the reference variable.

Syntax of Reference variable is very simple

//variable initialization
int vari = 10; //simple variable
int &refrenceVariable = vari; // reference variable

Memory Adress of "&refrenceVariable" variable and "Vari" variable will be same.

Example

Pass by Reference and value

  1. #include <iostream>  
  2. using namespace std; 
  3. int main( ){ 
  4.     
  5.    // declare simple variables
  6.    int intVari;
  7.    double doubleVari;
  8.  
  9.    // declare reference variables
  10.    int    &i = intVari;
  11.    double &d = doubleVari;
  12.    
  13.    i = 15;
  14.    cout << "Value of Integer Variable : " << intVari << endl;
  15.    cout << "Value of i reference Variable : " << i  << endl;
  16.    cout << "Memory Address of i : " << &i  << endl;
  17.    cout << "Memory Address of intVari : " << &intVari  << endl;
  18.  
  19.    d = 10.8;
  20.    cout << "Value of Double Variable : " << doubleVari << endl;
  21.    cout << "Value of d reference : " << d  << endl;
  22.    return 0;
  23. }
                                                Console :
                                                Value of Integer Variable : 15
                                                Value of i reference Variable : 15
                                                Memory Address of i : 0x7ffc78473fac
                                                Memory Address of intVari : 0x7ffc78473fac
                                                Value of Double Variable : 10.8
                                                Value of d reference : 10.8
                                                Watch Video
                                                C++ Pass by Ref and Value for Beginners Lecture in Urdu Hindi

                                                Comments

                                                Post a Comment

                                                Popular posts from this blog

                                                C++ Classes and Objects Programming

                                                Animated Gif of class and object C++ class and Object A class is a blue print and combination of Different functions and variables. The  difference between C and C++  is that  C  is a procedural  programming  and does not support classes and objects, while  C++  provide support of Object Oriented Programming(OOP) and classes  therefore  C++  can be called a hybrid language. The variables and functions within a class are called members(variables,functions) of the class. Syntax of C++ Class A class definition starts with the keyword  class. For example, we defined the Student data type using the keyword  class  as follows. class Student { public:   string name; // member variables   int rollNumber; } A public member can be accessed from outside the class anywhere within the scope of the class object. You can also specify the members of a class as  private  o...

                                                C++ Member Initializer Beginner Programming

                                                C++ Member initializer Animated gif example  Member initializer C++ Member initializer for Beginners Lecture