back
Passing by Reference
Recall that when we are dealing with functions, the information inside the parenthesis is called the parameter of the function. For example, in the function:

cube( int y)

int y is the parameter. When you use this function in your program, you can put any int variable inside the parenthesis. The function will still do its thing because that's what functions do. We call this "passing by value." This is what we want functions to do--usually. We want them to be self contained units that do the work we ask them to do.

There are times when we don't want this to happen. There are times when we want the function to to change the value. This is accomplished by an action called "passing by reference." We do this by declaring the variable with an ampersand right after the variable type. Like in this example:

cube(int& y).

The addition of the ampersand changes the way the parameter is passed. This is a little difficult to grasp now, but later when start game programming you will see the value of all this.

Here is an example of how it works:

[example]

Run the above program and observe the difference between passing by value and passing by reference.

Analogy
One way to help make this make more sense is to think of it this way: The main reason we use functions is that they return a value. We plug in some numbers and out comes a response processed by the function. Have you ever seen one of those automated bread machines? You know the kind where you add flour and water, plug it in and a few hours later you have bread? Well, that's what a function is like. You see it's designed to process flour and water and it's not that picky about the exact ingredients. You could, for example, use wheat flour instead of white flour. If you put gravel in it, it's not going to work. What you input will work as long as you stick to the correct data types. You don't design the function to use ints and then suddenly put string variables inside anymore than you'd buy a bread machine and then put marbles in it.

Functions Return a Value
Functions are designed to return one value, but what if your function needs to return more than one? Let's say you want to find the area and circumference of a circle, and you want to have it all done in one function. Can you do that? Well one way is to use reference parameters. Study this example:

[circle function]

Did you see how the function passed some of the parameters by reference and by value?

Okay, let's take it one step further. Now we are going to look at passing an array to a function. The key point when you are passing an array to a function is that the code to do this requires the array's name and type. It doesn't actually need to know the number of elements in an array. That information is usually provided in a separate int variable. For example, you can code:

double x[]

The actual function might look something like this:

void CalculateArray(double x[], int&y)

In this manner, the function directly connects to element's place in memory. Look at this example:

[example of passing an array to a function]

Assignment
Verbally explain first to another student and then to me, the difference between passing by value and passing by reference.

Homework
Read pages 184-189 in Knowlton. Do exercises 10-1 through 10-3.