Reference Parameters

See pages 283-289 in A Computer Science Tapestry for more information

Passing Parameters by Value

You may not have known it, but until now when you have passed variables in functions you have been passing them by value.  What this means is that you pass a value of a variable (i.e. a number, or a string) to the function, not the variable itself.  When you perform operations on the value inside the function it does not affect the value of the variable that it originally came from. This has been limiting because the only way to get values back from a function is to return them.   This is demonstrated in the following example.

#include <iostream.h>



float circle(float radius)

{

    cout<<"The perimeter is"<<3.14*2*radius<<endl;

    cout<<"The area is"<<3.14*radius*radius<<endl;

    radius=2*radius;

    cout<<"With twice the radius, the perimeters is"<<3.14*2*radius<<endl;

    cout<<"The area is"<<3.14*radius*radius<<endl;

    return radius;

}

int main(void)

{

    float radius, newradius;



    cout<<"Enter radius: ";

    cin>>radius;

    newradius=circle(radius);

    cout<<"Old radius is: "<<radius<<endl;

    cout<<"New radius is: "<<newradius<<endl;

    return 0;

}

Notice in this program that there is no way of returning the perimeter or area to the main part of the program as long as you are returning the radius.   Also note that doubling the radius int the circle function does not affect the value of the radius in the main function, yet this doubled value can be returned to the main function.  This is because we passed the value of the radius to the circle function, we did not pass the radius variable itself.

Passing Parameters by Reference

You can, however, pass the variables themselves to functions.   This is called passing parameters by reference and is simple to do.  All that needs to be done is to put an ampersand (&) symbol before the variable name in the function.   As in the last example, the circle function would instead be declared:

float circle(float &radius)

Try making this modification to the previous program, and answering the following questions.

  1. How do the values of new radius and radius change? 
  2. Can you change the name of "radius" in main without changing the name "radius" in the circle function?
  3. What does this tell you of the "scope" of variable names?   That is, does a variable name exist within the whole program, or does it exist just within a function?  What would happen if you defined radius as a global variable?   What purpose would this serve?

This same technique can be used to get the values of perimeters and areas back to main or back to another function.  For example the following function takes a value and squares it, the original value and the squared value are accessible to main.

#include <iostream.h>



void square(float value, float &squared)

{

    squared=value*value;

}



int main(void)

{

    float number, result;

    cout<<"Enter value: ";

    cin>>number;

    square(number, result);

    cout<<"Squared value: "<<result<<endl;

    return 0;

}

Notice the variable "result" in main does not have a value until it gets one in the square function.  Also notice that since the value of the variable "value" is not changed in square that it does not need to be passed by reference.  Finally, notice that the variable names in main do not have to coincide with the names used in the square function.  In fact it is useful to use different variable names so that the two sets of names do not get confused. 

Now, use this information on passing values by reference to redo the circle function so that the perimeter and area values are passed to a separate function that prints them out.  You should use different variable names in the circle function and the printing function.  You can also try to simplify the circle function so that is only prints out the perimeter and area, and the doubling of the radius could be done in the printing function.  Make sure you understand these concepts because they will be important for assignment 9 which is coming up.

Back to Home Page House3.wmf (25540 bytes)