Showing posts with label Reference. Show all posts
Showing posts with label Reference. Show all posts

Wednesday, September 29, 2010

Creating a Call by Reference

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() .


Note
: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 Value, Call by Reference

In a computer language, there are two ways that arguments can be passed to a
subroutine. The first is known as call by value. This method copies the value of an argument into the formal parameter of the subroutine. In this case, changes made to
the parameter have no effect on the argument.
Call by reference is the second way of passing arguments to a subroutine. In this
method, the address of an argument is copied into the parameter. Inside the subroutine,
the address is used to access the actual argument used in the call. This means that
changes made to the parameter affect the argument.
By default, C/C++ uses call by value to pass arguments. In general, this means that
code within a function cannot alter the arguments used to call the function. Consider
the following program:
#include <stdio.h>
int sqr(int x);
int main(void)
{
int t=10;
printf("%d %d", sqr(t), t);
return 0;
}
int sqr(int x)
{
x = x*x;
return(x);
}
In this example, the value of the argument to sqr() , 10, is copied into the parameter
x. When the assignment x = x*x takes place, only the local variable x is modified. The
variable t, used to call sqr() , still has the value 10. Hence, the output is 100 10.
Remember that it is a copy of the value of the argument that is passed into the
function. What occurs inside the function has no effect on the variable used in the call.

Monday, September 27, 2010

Call by Value, Call by Reference

In a computer language, there are two ways that arguments can be passed to a
subroutine. The first is known as call by value. This method copies the value of an argument into the formal parameter of the subroutine. In this case, changes made to
the parameter have no effect on the argument.
Call by reference is the second way of passing arguments to a subroutine. In this
method, the address of an argument is copied into the parameter. Inside the subroutine,
the address is used to access the actual argument used in the call. This means that
changes made to the parameter affect the argument.
By default, C/C++ uses call by value to pass arguments. In general, this means that
code within a function cannot alter the arguments used to call the function. Consider
the following program:
#include <stdio.h>
int sqr(int x);
int main(void)
{
int t=10;
printf("%d %d", sqr(t), t);
return 0;
}
int sqr(int x)
{
x = x*x;
return(x);
}
In this example, the value of the argument to sqr() , 10, is copied into the parameter
x. When the assignment x = x*x takes place, only the local variable x is modified. The
variable t, used to call sqr() , still has the value 10. Hence, the output is 100 10.
Remember that it is a copy of the value of the argument that is passed into the
function. What occurs inside the function has no effect on the variable used in the call.

Creating a Call by Reference in C,C++

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.