Parameter passed by value:View more random threads:
- Is there Any difference b/w Object Oriented ProgrAmming And...
- reenode* node = new treenode(info);
- what are the latest updates for C++ May 2011
- if we have a program to check, how we can tell how many...
- Constructor for Composition of inherited classin c++ 2011
- Define calling function in c++ 2011
- constructor and destructor in c++ 2011
- what is heap and why we use it
- Variable Access Speed in C++
- sir i cant under stand function and cant apply it in...
When we call a function with parameter we pass the copies of their values.
For example
Int x=2, y= 3, z;
z= addition (x,y);
What we did that we pass the value of x and y ( i.e. 2 and 3 )to our desired function.
Parameter passed by reference:
In parameter passed by reference, the first thing is that in the declaration of our function the type of each parameter was followed by an ampersand sign (&). This ampersand is what specifies that their corresponding arguments are to be passed by reference instead of by value. When a variable is passed by reference we are not passing a copy of its value, but we are somehow passing the variable itself to the function and any modification that we do to the local variables will have an effect in their counterpart variables passed as arguments in the call to the function.
Example
Sponsored Links
Void addition (int & a, int & b, int & c)
{
a * = 1;
b* = 2;
c* = 3;
}
Now when we call the function
int x=5, y=3, z=2;
addition (x, y, z);
Here x, y, z will be passed by reference to our original addition function.
There are currently 1 users browsing this thread. (0 members and 1 guests)