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:
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 ParameterNamewhere Type is a valid type, and ParameterName is a valid identifier.
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:
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
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 basic rules governing the scope of C++ names can be summarized as follows:
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:
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.
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 12: keywords, autos and externs
Argument, Parameter, Value Parameter, Reference Parameter, Scope, Auto, Extern.
Hard copies of your experimental hypotheses, observations and conclusions,
and your final version of params.cpp.
Forward to the Homework Projects
The Parameter Experiments
Part II: Scope in C++
The (Simplified) C++ Rules of Scope
C++ classes have their own rules of scope,
which we will examine in a later exercise.
for (int i = 1; i <= 10; i++)
cout << i << '\n';
is illegal in most languages, since the name i
is declared within the loop.
The Scope Experiments
Phrases you should now understand:
Submit:
Copyright 1998 by
Joel C. Adams. All rights reserved.