Lab 7: Parameter-Passing and Scope


Introduction

Today's exercise involves a series of experiments that investigate some of the features of C++ functions that we have ignored until now. The experiments are divided into two categories:

  1. 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. 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. For each experiment your instructor wishes you to complete, print a hard copy of its page on which to record your work.

Getting Started

Create a new directory to hold your work from this exercise, and save a copy of params.cpp there.

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 Parameter-Declarations separated by commas, each of which has the form:
   Type ParameterName
where Type is a valid type, and ParameterName is a valid identifier.

Terminology

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

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, and personalize its opening documentation. 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 (in some way) tries to modify the values of those variables;
  3. Output the values of those variables to view the effects of function Change().
Function Change() is the "laboratory" in which we will experiment with parameters.

 

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 telling the compiler a meaning for the name being declared.

However, think about this for a moment. In experiment 3, we saw that the name arg1 can be declared as an integer variable in the main program, and (again) as an integer value parameter in Change() -- two different places. Moreover, we 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.

That is, there is nothing that prevents us from declaring the name X as an integer variable in main and then redeclaring the same name X as a real variable in Change(). The same name will have one meaning when execution is in main, and an different meaning when execution is in Change().

Once we realize that the same name can have different meanings at different places, it becomes important to understand the rules by which C++ determines the meaning of a name at a given spot in one's program.

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. A name may be declared anywhere that a statement can appear.
  2. The scope of any name declared within a pair of braces starts at its declaration and ends at the close-brace (}). Such objects are described as auto (or sometimes local) names, because they automatically come into existence whenever execution enters the surrounding braces.
  3. The scope of any parameter starts at its declaration and ends at its function's close brace.
  4. The scope of any name declared outside of all pairs of braces starts at its declaration and ends at the end of the file. Such names are described as extern, since they are declared external to (i.e., outside of) all functions.
C++ classes have their own rules of scope, which we will examine in a later exercise.

Rule 1 sets C++ apart from other languages, since most languages restrict declarations to a particular region of your program (e.g., the beginning). For example, a statement like:

   for (int i = 1; i <= 10; i++)
      cout << i << '\n';
is illegal in most languages, since the name i is declared within the loop.

In the experiments that follow, we will examine the implications of the other rules.

Since that scope of a name is that portion of the program where the name has a particular meaning, the scope of an object's name is that part of the program where using the name accesses the object. That is, if we try and access an object outside of its scope, the compiler will generate an error message, but if we try to access the object within its scope, the access will succeed.

 

The Scope Experiments

Experiment 7: auto Scope, Part I

Experiment 8: auto Scope, Part II

Experiment 9: auto Scope, Part III

Experiment 10: auto Scope, Part IV

Experiment 11: extern Scope

Experiment 12: keywords, autos and externs

 

Phrases you should now understand:

Argument, Parameter, Value Parameter, Reference Parameter, Scope, Auto, Extern.


Submit:

Hard copies of your experimental hypotheses, observations and conclusions, and your final version of params.cpp.


Back to This Lab's Home Page

Back to the Prelab Questions

Forward to the Homework Projects


Copyright 1998 by Joel C. Adams. All rights reserved.