Even though C/C++ uses call by value for passing parameters, you can create a
call by reference by passing a pointer to an argument, instead of the argument itself.
Since the address of the argument is passed to the function, code within the function
can change the value of the argument outside the function.
Pointers are passed to functions just like any other value. Of course, you need
to declare the parameters as pointer types. For example, the function swap() , which exchanges the values of the two integer variables pointed to by its arguments,
shows how.
void swap(int *x, int *y)
{
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put x into y */
}
swap() is able to exchange the values of the two variables pointed to by x and y
because their addresses (not their values) are passed. Thus, within the function,
the contents of the variables can be accessed using standard pointer operations, and
the contents of the variables used to call the function are swapped.
Remember that swap() (or any other function that uses pointer parameters) must
be called with the addresses of the arguments. The following program shows the correct
way to call swap() :
void swap(int *x, int *y);
int main(void)
{
int i, j;
i = 10;
j = 20;
swap(&i, &j); /* pass the addresses of i and j */
return 0;
}
In this example, the variable i is assigned the value 10 and j is assigned the value
20. Then swap() is called with the addresses of i and j. (The unary operator & is used
to produce the address of the variables.) Therefore, the addresses of i and j, not their
values, are passed into the function swap() .
C++ allows you to fully automate a call by reference through the use of reference
parameters. This feature is described in Part Two.
call by reference by passing a pointer to an argument, instead of the argument itself.
Since the address of the argument is passed to the function, code within the function
can change the value of the argument outside the function.
Pointers are passed to functions just like any other value. Of course, you need
to declare the parameters as pointer types. For example, the function swap() , which exchanges the values of the two integer variables pointed to by its arguments,
shows how.
void swap(int *x, int *y)
{
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put x into y */
}
swap() is able to exchange the values of the two variables pointed to by x and y
because their addresses (not their values) are passed. Thus, within the function,
the contents of the variables can be accessed using standard pointer operations, and
the contents of the variables used to call the function are swapped.
Remember that swap() (or any other function that uses pointer parameters) must
be called with the addresses of the arguments. The following program shows the correct
way to call swap() :
void swap(int *x, int *y);
int main(void)
{
int i, j;
i = 10;
j = 20;
swap(&i, &j); /* pass the addresses of i and j */
return 0;
}
In this example, the variable i is assigned the value 10 and j is assigned the value
20. Then swap() is called with the addresses of i and j. (The unary operator & is used
to produce the address of the variables.) Therefore, the addresses of i and j, not their
values, are passed into the function swap() .
C++ allows you to fully automate a call by reference through the use of reference
parameters. This feature is described in Part Two.
No comments:
Post a Comment