Animated Gif Array in function Parameter
C++ Array in Function parameter
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
Comments
Post a Comment