Robert Ryan wrote:
> what I am trying to figure out: call by value - call by reference - calling a
> function or to call a function.
> to call by value or call by reference
do you use a pointer * or do you use &
Depends. In C, you can only do:
Swap(&a, &b);
void Swap(int *x, int *y)
In C++, you can let the compiler do the same thing much more easily:
Swap(a, b);
void Swap(int &x, int &y)
Produces the same effect, but the latter is safer to use...it is harder
to pass in an invalid pointer.
> I think 'value' is the copy of and 'reference' is the address of
> bob
Swap(a, b);
void Swap(int x, int y)
Will copy a and b to x and y respectively.
Swap(&a, &b);
void Swap(int *x, int *y)
Passes the address-of a and b to x and y respectively. This grants x
and y access to the location of the values that a and b point to.
Swap(a, b);
void Swap(int &x, int &y)
{
int t = x;
x = y;
y = t;
}
The compiler passes the address-of a and b but pointer access
(dereferencing) is completely transparent even within the Swap()
function itself.
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleSoft.com/MyTaskFocus/