Reference Parameters Continued

An Analogy

If you are a bit confused about the difference between reference and value parameters, think of the following analogy.  Passing a parameter by value is like passing a photocopy of a document to someone else.  If that person alters the copy of that document, the original is left unchanged.  Passing a parameter by reference, however, is like passing the original document itself.  If another person alters the original document in any way, those changes are still there when you get the document back.

Let's extend this analogy further to the example of the radius.  Passing the radius by value is like following this set of instructions:

  1. Write down the radius on a piece of paper

  2. Make a copy of the paper and hand it to a friend

  3. Have your friend double the number and write that number on the copy.

  4. Have your friend calculate the area and perimeter using that number, but don't write it down

  5. Have your friend pass the copy back to you.

From this example it is easy to see that you will be left with your original piece of paper with the original radius on it, and another paper with the doubled radius on it.

Modifying this example to parallel passing the radius by reference would leave this set of instructions:

  1. Write down the radius on a piece of paper

  2. Pass the paper to your friend

  3. Have your friend double the number and write that number on the paper.

  4. Have your friend calculate the area and perimeter using that number, but don't write it down

  5. Have your friend pass the paper back to you.

In this case, you will only have one copy of the paper that will only have the doubled value of the radius on it.  This is analogous to passing a parameter by reference.  You can use this technique to have functions pass back many results.  When you give the original paper with the radius on it  to your friend, you might also make boxes to hold additional numbers that your friend will calculate.  Your friend could then write the values of the perimeter and area in those boxes so that you know the values when the paper gets handed back.  This is analogous to defining variables without values and passing them to another function, as was shown yesterday in the square example.

An Exercise

(Taken from A Computer Science Tapestry page 290)

It is often necessary to interchange, or swap, the values of two variables.  For example, if a=5 and b=7, then swapping values would result in a=7 and b=5.  Write the body of the function Swap (Hint: You'll need to define a variable of type int).

    void Swap (int &a, int &b)

Variable Scope and Use

From what you have learned about passing values by reference and value, and what you already knew about defining variables globally and locally make a quick reference chart describing the use of these types of variables.   You should include information on how and where you declare these variable, what the syntax of the declaration is, what functions have access to the variables and when you think you should use them in programming.  A simple chart should accomplish this.

You should hand in the chart, the swap function and the modified circle function from yesterday to be graded as a mini-assignment.

Once you have completed these exercises you can proceed to some more information on visual programming.

Back to Home Page House3.wmf (25540 bytes)