Technically "call-by" is not talking about argument passing, but it is commonly used as such.
The issue at heart is, I think,
what am I passing to the function? And as consequent,
how does it get there?
Keeping it simple, we'll talk about
ints.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void by_value( int n )
{
n = 7;
}
void by_pointer( int* n ) // aka, pass by indirect reference
{
*n = 8;
}
void by_reference( int& n ) // aka, pass by direct reference, or pass by alias
{
n = 9;
}
#include <iostream>
using std::cout;
int main()
{
int n = 2;
by_value( n ); cout << n << endl;
by_pointer( &n ); cout << n << endl;
by_reference( n ); cout << n << endl;
}
|
2
8
9 |
We all understand pass-by-value: a copy of
n's
value is passed as argument to the function -- hence the function as a local copy of
n to play with.
We all understand pass-by-(direct)-reference / pass-by-alias: an alias for
n is passed as argument to the function -- hence the function's
n is (for all intents and purposes) the
same thing as
main()'s
n.
But somehow when we start talking about pass-by-(indirect)-reference, we all seem to start paying too much attention to the trees. But we are
still passing that
n into the function, only we have done it by passing its address, such that the function's formal parameter is a pointer, which we must explicitly dereference to get at
main()'s
n.
Here is where we must pay attention to the formal arguments, and it is where
Grey Wolf's information has merit. The
address of
n is passed by
value. We must dereference that value (the address) to get at the object we want to view/manipulate.