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.
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.
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.
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
- #include <iostream>
- using namespace std;
- int main( ){
- // declare simple variables
- int intVari;
- double doubleVari;
- // declare reference variables
- int &i = intVari;
- double &d = doubleVari;
- i = 15;
- cout << "Value of Integer Variable : " << intVari << endl;
- cout << "Value of i reference Variable : " << i << endl;
- cout << "Memory Address of i : " << &i << endl;
- cout << "Memory Address of intVari : " << &intVari << endl;
- d = 10.8;
- cout << "Value of Double Variable : " << doubleVari << endl;
- cout << "Value of d reference : " << d << endl;
- return 0;
- }
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
I appreciate you for this blog. More informative, thanks for sharing with us.
ReplyDeleteC C++ Training in Chennai
c c++ courses in chennai
C C++ Training in T Nagar
JMeter Training in Chennai
JMeter Course
Appium Training in Chennai
javascript training in chennai
core java training in chennai
I really enjoyed this article. I need more information to learn so kindly update it.
ReplyDeleteSelenium Training in Chennai
Selenium Course in Chennai
selenium course
Selenium Training Institute in Chennai
Selenium training in Guindy
Selenium Training in Tambaram
Python Training in Chennai
Big data training in chennai
SEO training in chennai
JAVA Training in Chennai
I got wonderful information from this blog. Thanks for sharing this post. it becomes easy to read and understand the information.
ReplyDeleteData Science Course in Chennai
Data Science Courses in Bangalore
Data Science Course in coimbatore
Data Science Course in Hyderabad
Data Science Training in Bangalore
Best Data Science Courses in Bangalore
Data Science Institute in Bangalore
Data Science Training Institutes in Bangalore
AWS Training in Bangalore
DevOps Training in Bangalore