[ fromfile: functions.xml id: valueparm ]
By default, C++ parameters are passed by value.
When a function is called, a temporary (local) copy of each argument object is made and placed on the program stack.
Only the local copy is manipulated inside the function, and the argument objects from the calling block are not affected by these manipulations.
The temporary stack variables are all destroyed when the function returns.
Example 5.11 gives a short demonstration.
Example 5.11. src/functions/summit.cpp
If a pointer is passed to a function, a temporary copy of that pointer is placed on the stack.
Changes to that pointer will have no effect on the pointer in the calling block.
Example 5.12. src/functions/pointerparam.cpp
#include <iostream> using namespace std; void messAround(int* ptr) { *ptr = 34; ptr = 0; } int main() { int n(12); int* pn(&n); cout << "n = " << n << "\tpn = " << pn << endl; messAround(pn); cout << "n = " << n << "\tpn = " << pn << endl; return 0; } Output: n = 12 pn = 0xbffff524 n = 34 pn = 0xbffff524
Note | |
---|---|
To summarize:
|
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |