1.4d — A first look at local scope

You already saw in the lesson 1.3 -- a first look at variables 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:

  • main() is executed
  • main’s variable a is created and given value 5
  • main’s variable b is created and given value 6
  • function add() is called with values 5 and 6 for arguments
  • add’s variable x is created and given value 5
  • add’s variable y is created and given value 6
  • operator + adds 5 and 6 to produce the value 11
  • add returns the value 11 to the caller (main)
  • add’s x and y are destroyed
  • main prints 11 to the console
  • main returns 0 to the operating system
  • main’s a and b are destroyed

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 and function calls, 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

1.5 -- A first look at operators
1.4c -- Keywords and naming identifiers

132 comments to 1.4d — A first look at local scope

  • Hans

    Helo there, first of all thanks for making this site. I learn so much here. I wanna ask something about the answer for this quiz. Why does the variable x in void doIt has value of 1? void doIt(int x) didn't give it value so shouldn't it become unpredictable?

  • HEY
    if nothing is initialized in int x= {nothing is assigned here to x}
    what will be the value given by compiler to X???

  • Kio

    Hi Alex,

    Maybe some kind of suggestion. What do you think,
    using same example below this

    additional print address of the variable x and y (so beginners can see, that their addresses are different). Or maybe this is confusing?

Leave a Comment

Put all code inside code tags: [code]your code here[/code]