Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is meant by "pass by value and pass by reference" in c++?
#1
Parameter passed by value:


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

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.

0300-9520262
Get solution in 24 hour
|cpmputer & internetConfusedmileys81:[SIZE="4"][SIZE="3"]....[/SIZE][/SIZE]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  what is meant by trivial and non trivial in C and C++ 2012 Vuhelper 0 4,057 05-31-2012, 01:38 AM
Last Post: Vuhelper
  call by value, call by reference,call by pointer in data structure 2012 Vuhelper 0 4,333 01-04-2012, 10:28 PM
Last Post: Vuhelper
  how to pass a simlpe list of data type struct to function in c++ 2011 Vuhelper 0 859 07-20-2011, 10:49 PM
Last Post: Vuhelper
  Passing 2d array by reference June 2011 Vuhelper 0 920 06-19-2011, 06:11 PM
Last Post: Vuhelper
  Pass string value into function as an array in c++ Vuhelper 0 952 06-11-2011, 06:42 PM
Last Post: Vuhelper
  Define Call by reference in c++ 2011 Vuhelper 0 3,246 06-01-2011, 10:58 PM
Last Post: Vuhelper
  why we use pointers or reference except using real name of var. what is buffer point saneha 1 9,881 05-02-2011, 06:24 PM
Last Post: Vuhelper
  why we use pointers or reference except using real name of var. what is buffer point saneha 1 5,227 05-02-2011, 06:05 PM
Last Post: Vuhelper
  what is meant by concrete class in c++ 2011? saneha 1 3,866 05-02-2011, 06:25 AM
Last Post: Vuhelper
  How to call by reference ? I have an error. sana.pari 1 1,633 04-24-2011, 05:55 PM
Last Post: Support

Forum Jump:


Users browsing this thread: 1 Guest(s)