Skip to main content

How To Think Like a Computer Scientist C++ Edition The Pretext Interactive Version

Section 3.9 Parameters and Variables are Local

Parameters and variables only exist inside their own functions. In the code below, within the confines of main, there is no such thing as phil. If you try to use it, the compiler will complain. Similarly, inside printTwice there is no such thing as argument.
Listing 3.9.1. The following code will show the output of the printTwice function. Notice that it is the argument ‘b’ that is outputted, not the variable ‘phil’.
Variables like this are said to be local. In order to keep track of parameters and local variables, it is useful to draw a stack diagram. Like state diagrams, stack diagrams show the value of each variable, but the variables are contained in larger boxes that indicate which function they belong to.
For example, the state diagram for printTwice looks like this:
described in detail following the image
Two boxes. One labeled main that contains a variable named ’argument’ which is ’b’. The other labeled printTwice that contains a variable named ’phil’ which is ’b’.
Figure 3.9.2. Local variable stack diagram
Whenever a function is called, it creates a new instance of that function. Each instance of a function contains the parameters and local variables for that function. In the diagram an instance of a function is represented by a box with the name of the function on the outside and the variables and parameters inside.

Note 3.9.1.

When you use the Codelens feature to run a program, it shows the variables organized in a stack diagram. Try it on Listing 3.9.1 above.
In the example, main has one local variable, argument, and no parameters. printTwice has no local variables and one parameter, named phil.

Checkpoint 3.9.1.

How many local variables and parameters does main have?
void prprintHelloName(string name) {
  cout << "Hello " << name << "!";
}

int main() {
  string name1 = "Phil";
  printHelloName(name1);
  string name2 = "Joe";
  printHelloName(name2);
  return 0;
}
  • 1 local variable, 1 parameter
  • A parameter would be located within the parentheses next to the function’s name.
  • 0 local variables, 1 parameter
  • A parameter would be located within the parentheses next to the function’s name.
  • 2 local variables, 0 parameters
  • Correct!
  • 2 local variables, 1 parameter
  • A parameter would be located within the parentheses next to the function’s name.

Checkpoint 3.9.2.

How many local variables and parameters does printHelloName have?
void prprintHelloName(string name) {
  cout << "Hello " << name << "!";
}

int main() {
  string name1 = "Phil";
  printHelloName(name1);
  string name2 = "Joe";
  printHelloName(name2);
  return 0;
}
  • 1 local variable, 1 parameter
  • A local variable exists when a variable is declared within a function.
  • 0 local variables, 1 parameter
  • Correct!
  • 2 local variables, 0 parameters
  • A local variable exists when a variable is declared within a function.
  • 2 local variables, 1 parameter
  • A local variable exists when a variable is declared within a function.

Checkpoint 3.9.3.

Checkpoint 3.9.4.

How many calls to hi are made during the exectuion of the entire program?
void hi() {
  cout << "hiii !"<<endl;
}

void printGreeting() {
  hi();
  cout<<"how are you doing today. "<<endl;
  hi();
}

int main() {
  hi();
  printGreeting();
  hi();
  return 0;
}
  • 1 call
  • hi( ) is called from multiple functions.
  • 4 calls
  • Correct!
  • 2 calls
  • hi( ) is called from multiple functions.
  • 3 calls
  • Two calls from one function are indeed two seperate calls.
You have attempted 1 of 4 activities on this page.