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:
-
A name may be declared anywhere that a statement can appear.
-
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.
-
The scope of any parameter starts at its declaration
and ends at its function's close brace.
-
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.