Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call by Value in c++ 2011
#1
[Image: images?q=tbn:ANd9GcRMsoS72i1mLzioxggBXb0...9y2zpsWXA_]
Call by value : its means when we call a function and pass some argument to it we are passing the copy of the argument instead of original variable




Call by value:

1
2
3
4
5
6
7
void foo( int x ) {
cout << x << endl;
x = 4;
}

int someVar = 5;
foo( someVar );


The value 5 is pushed onto the stack and foo is called. Inside foo(), 5 is popped off the stack and output. x = 4 modifies the stack copy, which is then thrown away when the function returns.

1
2
3
4
5
6
7
void foo( int& x ) {
cout << x << endl;
x = 4;
}

int someVar = 5;
foo( someVar );


The address of someVar is pushed onto the stack and foo is called. Inside foo, the address is popped off the stack and the integer stored at that address is output. x = 4 modifies the memory referred to by the address, which means that when foo() returns, someVar now has the value 4.

Contrast the above code to

1
2
3
4
5
6
7
void foo( int* x ) {
cout << *x << endl;
*x = 4;
}

int someVar = 5;
foo( &someVar );

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
  call by value, call by reference,call by pointer in data structure 2012 Vuhelper 0 4,332 01-04-2012, 10:28 PM
Last Post: Vuhelper
  array project code c++ 2011 Vuhelper 0 1,274 12-10-2011, 06:32 PM
Last Post: Vuhelper
  factorial in C++ code 2011 Vuhelper 0 945 12-10-2011, 06:31 PM
Last Post: Vuhelper
  stack during function call in C++ 2011 Vuhelper 0 2,027 11-05-2011, 05:05 PM
Last Post: Vuhelper
  What is the Thing Queue? in c++ 2011 Vuhelper 0 1,785 11-05-2011, 05:02 PM
Last Post: Vuhelper
  ASCII to char conversion code in C++ 2011 Vuhelper 0 1,721 11-05-2011, 05:01 PM
Last Post: Vuhelper
  How to write a summation in a sequence? C++ 2011-12 Vuhelper 2 1,355 09-19-2011, 04:53 AM
Last Post: Xpert
  GetPixel problem in c++ 2011-12 Vuhelper 0 2,507 09-18-2011, 07:12 PM
Last Post: Vuhelper
  C++ with loop code 2011-12 Vuhelper 0 978 09-18-2011, 07:09 PM
Last Post: Vuhelper
  how can i read the last letter of a input file? 2011 Vuhelper 0 1,832 09-12-2011, 10:24 PM
Last Post: Vuhelper

Forum Jump:


Users browsing this thread: 1 Guest(s)