Passing By Value
Passing by value is easy, infact you have already passed arguments to functions by value many times.
Here is what you do:
 | |  | | |
//pass by value
#include <iostream.h>
// an example function
void printage(int num)
{
cout << num << endl;
}
int main(void)
{
int myage = 18;
printage(myage); // pass "age" by value
return 0;
} |
|  |
|
What passing by value does is that it copies your variable to a temporary variable and uses that copy instead of the original variable, thus assuring that your original variable will not get altared.
This is very usefull when sending small types of variables to a function, like most of the basic types: int, char, long, float, etc... But when you are trying to send large amounts of data, passing by value will not be a good idea since it produces an internal copy of the data.
For those purposes, the next section describes how you can pass your variable by reference.Please Rate this Code: