int main(int argc, char *argv[])
{
int x, y;
void f(int, int); // pass by value
x = 10;
y = 100;
cout << "Before f(x,y), x = " << x << ", y = " << y << "\n";
f(x, y);
cout << "After f(x,y), x = " << x << ", y = " << y << "\n";
}
void f(int a, int b)
{
cout << "Before increment a = " << a << ", b = " << b << "\n";
a = a + 10000;
b = b + 10000;
cout << "After increment a = " << a << ", b = " << b << "\n";
}
|
The following figure shows what is going on inside the computer when parameters are passed "by value":
|
int main(int argc, char *argv[])
{
int x, y;
void f(int &, int &); // pass by reference
x = 10;
y = 100;
cout << "Before f(x,y), x = " << x << ", y = " << y << "\n";
f(x, y);
cout << "After f(x,y), x = " << x << ", y = " << y << "\n";
}
void f(int & a, int & b)
{
cout << "Before increment a = " << a << ", b = " << b << "\n";
a = a + 10000;
b = b + 10000;
cout << "After increment a = " << a << ", b = " << b << "\n";
}
|
The following figure shows what is going on inside the computer when parameters are passed "by reference":
|