Monday, 11 February 2013

Pass-by-value vs. pass-by-reference


Pass-by-value vs. pass-by-reference

By default, the variables declared in the argument list of a function de nition are local
variables to that function. When a function is called, its arguments are copied into the
argument variables. The original variables, in the calling program, are not a ected by any
changes made by the function to its argument variables. This style of argument passing is
pass-by-value.
Alternatively, the programmer may wish for changes made by the function to one or more
of its arguments to be re
ected in the corresponding variables of the calling program. To
accomplish this, when the variable is passed into the function through one of its arguments,
the copy process is skipped and the function has access to the actual variable as it exists
in the calling program. This style of argument passing is pass-by-reference. The syntax
to inform the compiler what style of argument passing is desired is to simply precede the
argument name with a '&' character.
Pass-by-reference is particularly useful when the programmer wishes to have a function
return more than one piece of information to the calling program. For example, suppose in
our Mean() function we wished, not only to have the mean returned to the calling program,
but also to have the function give us the sum of the two arguments. A simple return
statement is unable to return more than one value. To get around this, consider the function
in Listing 4.8.
Compare this to the function in Listing 4.7. The return type is now void and there are
two pass-by-reference arguments. Observe the use of the '&' character in the second two
arguments to indicate that these arguments are pass-by-reference. Listing 4.9 shows how to
use this function to get both the sum and the mean of the rst two arguments.
The main program declares 4
oat-type variables. The x and y variables hold the numbers
to be summed and averaged. Notice that we also need variables, s and m, to hold the results of
the MeanSum() function. As the instructions of the main program are carried out from top to
bottom, before the call to MeanSum(), the variables s and m are empty (uninitialized). After
the call to MeanSum(), they contain the sum and mean respectively. Only after MeanSum()
has been allowed to do its work, can we display the contents of s and m. If we mistakenly try
to display the contents of s and m before the call to MeanSum(), the results are unpredictable{
we may just see a zero, or we may see some random number.



// computes the sum and the mean of its
// first two arguments , the sum is placed
// in the third argument , the mean is
// placed in the fourth argument
void MeanSum ( float a, float b, float &sum ,
float & mean ) {
sum = a+b;
mean = (a+b )/2;
return ;
}

Listing 4.8: A function that accepts two pass-by-value
oat-type arguments and two pass-
by-reference arguments, computes the sum and mean, and puts the results into the pass-by-
reference arguments.

task main () {
float x, y, s, m;
x =3.4; y =2.8;
MeanSum (x,y,s,m);
nxtDisplayString (0, " Sum = %5.2 f", s);
nxtDisplayString (1, " Mean = %5.2 f", m);
wait10Msec (200);
}

Listing 4.9: A main program that uses the MeanSum() function and displays the results.
More on RobotC function syntax can be found here2.

No comments:

Post a Comment