PC Labs in the Engineering Center
CR 235 CR 239
CR 244 CR 252 (24 hours)
CH 107 ME 107
Other campus sites are listed at www.colorado.edu/its/labs

CSCI 1300 - Exercise 4: More about Program Design and Functions

What You'll Get From This Exercise

This exercise is to supplement your reading on functions and parameters.

A program to Illustrate Parameters

Getting the Source File and Creating the Project
Start this exercise by opening Visual Studio and creating a new "CLR Empty Project." Keep track of the directory where the project is located. Once you've created the empty project, download the following file, and save it into that directory:

http://www.cs.colorado.edu/~main/intro/lab/params.cxx

In Visual Studio, right-click on the "Source Files" folder in Solution Explorer window, and select "Add => Existing Item." Find the "params.cxx" file and include it in your project.

The Purpose of params.cxx
The purpose of params.cxx is to show the difference between ordinary value arguments and a new kind or argument called a reference argument. Apart from that, the program doesnít really do anything useful. You can look through the code to get an idea of what the program is doing.

Illustrating a Value Parameter with the Debugger
The program reads an integer into a variable called favorite. The program then uses the integer in a message. For example, if you type 42. Then the program will write:

Yes, 42 is a fine number.

Compile the program, then use the debugger to put a breakpoint on the first line of the program. Then execute the program until the message asking for a number is printed. Use the number 42 as the input, and continue executing until the the highlighted line is the function call:

increment_and_print(favorite);

Now, before we actually execute the increment_and_print function, you should put watches on the favorite variable. It should have a value of 42. Then step into the function.

When you step into the function, the debugger will move to the start of the increment_and_print function, and favorite is no longer shown because it is not available in the function. From inside the function, the variable favorite is not available (since it is a local variable of the main function, and only available when we are actually inside the main function).

However, you can now put a watch on the variable i. The variable favorite provided the initial value (42) for i, but there is no longer any connection between i and favorite variables. We can execute the rest of the function, changing i to 43, and the value of favorite remains unchanged. Do this now, executing statements until you get back to the main function. You should have seen i change to 43, and when you got back to the main function favorite is still 42. This is the way that an ordinary argument works: The actual argument merely provides the starting value for the formal argument of the function.

Continue to execute statements until the program ends.

Changing to a Reference Parameter
The ordinary parameter that you just saw is called a value parameter. You will now change the parameter to a second kind of parameter: a reference parameter. Two changes are needed. First, find the prototype of the increment_and_print function, at the top of the main program. Insert the symbol & as shown here:

void increment_and_print(int& i);

Next, go down to the functionís implementation and change the parameter list there too, adding the symbol & These two changes make the parameter into a reference parameter. Hereís the key idea for a reference parameter:

With a reference parameter, every use of the parameter inside the function will refer to the actual argument back in the main program. If the parameter is changed inside the function, then this change will effect the actual argument in the main program. Information passes into the function through the initial value of the argument, but information can also pass out of the function through changes that are made to the argument while the function is running.


Illustrating a Reference Parameter with the Debugger
Now, compile and run your program again. When you reach the breakpoint at the start of the main program, step through the program one line at a time. As you are executing, make sure to display favorite in the main function. Stop when you reach the function call:

increment_and_print(favorite);

At this point, favorite is 42. Now, step into the function and display i. Do this now and you will see the value of *i displayed as 42.

Now, as you execute the statements of the function, i will change to 43. Although you cannot see it in the display, the actual argument favorite has also changed to 43 in the main function. In fact, the reason for this change is that the computer is using the same memory location for both favorite and i. When you return to the main function you will see the value of favorite is now changed to 43.

Thatís all there is to it. You now know the two most important kinds of parameters.