Lab 8: Parameter-Passing and Scope


Introduction

This lab's exercise involves a series of experiments that investigate some of the features and restrictions of C++ functions that we have glossed over until now. The experiments are divided into two categories:

  1. Parameters. In the first part, we will begin by exploring the nature of function parameters, and what rules govern the relationship between parameters and their arguments. We will also examine the different kinds of parameters available in C++.
  2. Scope. In the second part, we will examine the relationship between definitions that appear in different functions, and some of the rules of scope in C++.
There is a different page for each experiment which requires you to write out answers for your work. Write, or better yet type, your answers up and hand them in to your instructor. Your instructor may also want you to hand in your program after each experiment.

Experiments

Each experiment for this lab starts out with an issue and a hypothesis about the issue. Your work will be to prove or disprove the hypothesis.

Did you catch that, "prove or disprove"? The hypothesis might be wrong.

Files

Directory: lab8

Create the specified directory, and copy the files above into the new directory. Only gcc users need a makefile; all others should create a project and add all of the .cpp files to it.

Add your name, date, and purpose to the opening documentation of the code and documentation files; if you're modifying and adding to the code written by someone else, add your data as part of the file's modification history.

Part I: Parameter-Passing Mechanisms

The general form of a C++ function heading is:

ReturnType Name ( ParameterDeclarationList )
where ParameterDeclarationList is an optional sequence of one or more ParameterDeclarations separated by commas, each of which has the form:
Type ParameterName
where Type is a valid type, and ParameterName is a valid identifier.

We've used this in both the prototype and definition of our functions.

Terminology

  1. A value parameter is a parameter whose Type is not followed by an ampersand (&). A value parameter is a variable that is local to the function; when the function is called, it receives a copy of the value of the corresponding argument.
  2. A reference parameter is parameter whose Type is followed by an ampersand (&). (Technically, the ampersand is part of the type.) A reference parameter is an alias (e.g., another name) for its corresponding argument.

Question #8.1: We have used parameters in several of our programs. In every case, which kind did we use?

Using your editor, take a moment to look over the program in params.cpp. Its behavior consists of 3 steps:

  1. Initialize a set of variables to some initial value (-1 in this case).
  2. Call function change() that tries to modify the values of those variables.
  3. Output the values of those variables to view the effects of function change().

The function change() is the "laboratory" in which we will experiment with parameters. The key word above is "tries". change() tries to alter the variables in main() through its parameters, but it's not always successful.

The Parameter Experiments

Experiment #1: Changing Value Parameters

Experiment #2: Value Parameters and Their Arguments

Experiment #3: Naming Parameters and Arguments

Experiment #4: Differing Numbers of Parameters and Arguments

Experiment #5: Reference Parameters

Experiment #6: Constant Reference Parameters


Part II: Scope in C++

A declaration can be thought of as giving the compiler a meaning for the name being declared.

Think about this for a moment. In Experiment #3, we saw that the name arg1 can be declared twice: once as an integer variable in the main program, and a second time as an integer value parameter in change(). We also saw that when arg1 is a value parameter, altering it in change() leaves the value of arg1 in the main program unchanged. We might conclude from this that arg1 in main and arg1 in change() are two different variables.

Put differently, the same name can have different meanings in different places in a program.

There is nothing that prevents us from declaring any variable as an integer in main and then redeclaring that variable with the same name as a real number in change(). The same name will have one meaning when execution is in main, and an different meaning when execution is in change().

Keep in mind that a compiler hates ambiguity. It cannot tolerant confusion. So there must be some underlying rules that keep the two declarations separate. These rules are known as the "rules of scope".

Definition: The set of all places in a program where a name has a particular meaning is called the scope of that name.

The (Simplified) C++ Rules of Scope

The basic rules governing the scope of C++ names can be summarized as follows:

  1. Local block scope. The scope of any name declared within a block starts at its declaration and ends at the close-brace (i.e., }) of the block. The variable is defined in any of the code that appears in the program text between its declaration and the closing brace (including nested compound statements). These variables are called "local" because they automatically come into existence whenever execution enters the surrounding braces.
  2. Local parameter scope. The scope of a parameter starts at its declaration and ends at the function's close brace. Parameters are also local because their scope covers only a local area of the code.
  3. for-loop scope. A variable declared in the initialization expression of a for loop is local to just the for loop.
  4. Non-local scope. There are many other types of scope that we'll just label as "non-local". In particular, the scope of a variable declared outside any curly-brace blocks lasts from the variable's declaration to the end of the file.

C++ classes have their own rules of scope, which we will examine in a later exercise.

A variable's scope determines when it's valid. So if we try to access a variable outside its scope, the access is invalid, and the compiler will complain.

The Scope Experiments

Experiment #7: Same Identifier Declared Twice in Same Local Scope

Experiment #8: Same Identifier Declared in Nested Blocks

Experiment #9: Using an Twice-Declared Identifier in Inner Block

Experiment #10: Using an Twice-Declared Identifier in Outer Block

Experiment #11: Non-local Scope

Experiment #12: Keywords, Locals, and Non-Locals  

Submit

Turn in the answers to the questions of the experiments that you run. Your instructor may also want you to turn in a copy of your program after each experiment.

Terminology

for-loop scope, local block scope, local parameter scope, non-local scope, parameter, reference parameter, rules of scope, scope, scope, value parameter
Lab Home Page | Prelab Questions | Homework Projects
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.