- Learn C++ - http://www.learncpp.com -

1.4d — A first look at local scope

You already saw in the lesson 1.3 -- a first look at variables [1] that when the CPU executes a statement like int x;, this causes the variable to be instantiated (created).

The natural follow-up question is, “so when is an instantiated variable destroyed?”

Appetite for destruction

A variable’s scope determines when a variable can be seen and used during the time it is instantiated. Function parameters and variables defined inside the function body both have local scope. That is, those variables can only be seen and used within the function that defines them. Local variables are created at the point of definition, and destroyed when they go out of scope (usually at the end of the function).

Here’s a simple example:

Here’s a slightly more complex example:

In function add(), parameters x and y are created when the function is called, can only be seen/used within function add(), and are destroyed at the end of the add() function.
Variable a and b are created within function main(), can only be seen/used within function main(), and are destroyed at the end of main().

To enhance your understanding, let’s trace through this program in a little more detail. The following happens, in order:

And we’re done.

Note that if function add() were to be called twice, function add() parameters x and y would be created and destroyed twice -- once for each call. In a program with lots of functions, variables are created and destroyed often.

In the above example, it’s easy to see that variables a and b are different variables from x and y.

Now consider the following program:

In this example, all we’ve done is change the names of variables a and b inside of main() to x and y. This program still runs fine, even though both main() and add() both have variables named x and y. Why does this work?

First, we need to recognize that even though main() and add() both have variables named x and y, these variables are distinct. The x and y in main() have nothing to do with the x and y in add() -- they just happen to reuse the same names.

Second, when inside of main(), the names x and y refer to the locally scoped variables x and y. Those variables can only be seen (and used) inside of main(). Similarly, when inside function add(), the names x and y refer to function parameters x and y, which can only be seen (and used) inside of add().

In short, neither add() nor main() know that the other function has variables with the same names, and it’s always clear to the compiler which x and y are being referred to at any time. This means that functions don’t need to know or care what other functions name their variables, which is fantastic for us because it makes life easy.

We’ll talk more about local scope, and other kinds of scope, in chapter 4.

Rule: Names used for function parameters or variables declared in a function body are only visible within the function that declares them.


1) What does the following program print?

Quiz answers

1) Show Solution [2]

1.5 -- A first look at operators [3]
Index [4]
1.4c -- Keywords and naming identifiers [5]