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.
params.cppis our playground for the experiments in this lab exercise.
gccusers need a makefile; all others should create a project and add all of the
.cppfiles 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:
ParameterDeclarationListis an optional sequence of one or more
ParameterDeclarations separated by commas, each of which has the form:
Typeis a valid type, and
ParameterNameis a valid identifier.
We've used this in both the prototype and definition of our functions.
Typeis 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.
Typeis 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() is the "laboratory" in which we will
experiment with parameters. The key word above is "tries".
change() tries to alter the variables in
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
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
arg1 can be declared twice: once as an integer
variable in the main program, and a second time as an integer value
change(). We also saw that when
a value parameter, altering it in
change() leaves the value
arg1 in the main program unchanged. We might conclude
from this that
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
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
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
forloop is local to just the
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
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