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:
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.
Directory: lab8
params.cpp
is our playground for the experiments
in this lab exercise.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.
The general form of a C++ function heading is:
whereReturnType
Name
(ParameterDeclarationList
)
ParameterDeclarationList
is an optional sequence of
one or more ParameterDeclaration
s separated by commas,
each of which has the form:
whereType
ParameterName
Type
is a valid type, and ParameterName
is a valid identifier.
We've used this in both the prototype and definition of our functions.
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.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:
change()
that tries to modify the
values of those variables.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.
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 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 basic rules governing the scope of C++ names can be summarized as follows:
}
) 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.for
-loop scope. A variable declared in the initialization
expression of a for
loop is local to just the for
loop.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.
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
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.
for
-loop scope, local block scope, local parameter scope, non-local scope, parameter, reference parameter, rules of scope, scope, scope, value parameter