Search

2.4 — Introduction to local scope

Local variables

Function parameters, as well as variables defined inside the function body, are called local variables (as opposed to global variables, which we’ll discuss in a future chapter).

In this lesson, we’ll take a look at some properties of local variables in more detail.

Local variable lifetime

In lesson 1.3 -- Introduction to variables, we discussed how a variable definition such as int x; causes the variable to be instantiated (created) when this statement is executed. Function parameters are created and initialized when the function is entered, and variables within the function body are created and initialized at the point of definition.

For example:

The natural follow-up question is, “so when is an instantiated variable destroyed?”. Local variables are destroyed in the opposite order of creation at the end of the set of curly braces in which they are defined (or for function parameters, at the end of the function).

Much like a person’s lifetime is defined to be the time between their birth and death, an object’s lifetime is defined to be the time between its creation and destruction. Note that variable creation and destruction happen when the program is running (called runtime), not at compile time. Therefore, lifetime is a runtime property.

For advanced readers

The above rules around creation, initialization, and destruction are guarantees. That is, objects must be created and initialized no later than the point of definition, and destroyed no earlier than the end of the set of the curly braces in which they are defined (or, for function parameters, at the end of the function).

In actuality, the C++ specification gives compilers a lot of flexibility to determine when local variables are created and destroyed. Objects may be created earlier, or destroyed later for optimization purposes. Most often, local variables are created when the function is entered, and destroyed in the opposite order of creation when the function is exited. We’ll discuss this in more detail in a future lesson, when we talk about the call stack.

Here’s a slightly more complex program demonstrating the lifetime of a variable named x:

In the above program, x’s lifetime runs from the point of definition to the end of function main. This includes the time spent during the execution of function doSomething.

Local scope

An identifier’s scope determines where the identifier can be accessed within the source code. When an identifier can be accessed, we say it is in scope. When an identifier can not be accessed, we say it is out of scope. Scope is a compile-time property, and trying to use an identifier when it is not in scope will result in a compile error.

A local variable’s scope begins at the point of variable definition, and stops at the end of the set of curly braces in which they are defined (or for function parameters, at the end of the function). This ensures variables can not be used before the point of definition (even if the compiler opts to create them before then).

Here’s a program demonstrating the scope of a variable named x:

In the above program, variable x enters scope at the point of definition and goes out of scope at the end of the main function. Note that variable x is not in scope anywhere inside of function doSomething. The fact that function main calls function doSomething is irrelevant in this context.

Note that local variables have the same definitions for scope and lifetime. For local variables, scope and lifetime are linked -- that is, a variable’s lifetime starts when it enters scope, and ends when it goes out of scope.

Another example

Here’s a slightly more complex example. Remember, lifetime is a runtime property, and scope is a compile-time property, so although we are talking about both in the same program, they are enforced at different points.

Parameters x and y are created when the add function is called, can only be seen/used within function add, and are destroyed at the end of add. Variables 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 of how all this fits together, let’s trace through this program in a little more detail. The following happens, in order:

  • execution starts at the top of main
  • 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 initialized with value 5
  • add‘s variable y is created and initialized with value 6
  • operator+ evaluates expression x + y to produce the value 11
  • add copies the value 11 back to caller main
  • add‘s y and x are destroyed
  • main prints 11 to the console
  • main returns 0 to the operating system
  • main‘s b and a are destroyed

And we’re done.

Note that if function add were to be called twice, 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.

Functional separation

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 similar program:

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

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

Second, when inside of function main, the names x and y refer to main’s 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. Because the scopes don’t overlap, it’s always clear to the compiler which x and y are being referred to at any time.

Key insight

Names used for function parameters or variables declared in a function body are only visible within the function that declares them. This means local variables within a function can be named without regard for the names of variables in other functions. This helps keep functions independent.

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

Quiz time

Question #1

What does the following program print?

Show Solution


2.5 -- Why functions are useful, and how to use them effectively
Index
2.3 -- Introduction to function parameters and arguments

152 comments to 2.4 — Introduction to local scope

  • Singh

    I am getting below the result of running a shared program. Just curious to know what made the malfunction while running below code whereas its working fine with other two options.

    OUTPUT:
    "Please add both numbers.
    A: 5
    B: 6
    Addition of both 5 and 6 is 116296576

    Program Finished."

    [code]
    #include <iostream>

    int addfunction(int x, int y)
    {  
       std::cout<<"Addition of both " <<x <<" and " <<y <<" is " <<x+y;
       /* why can't we use above where as both the belw sections works fine.
        instead of
        int c{x+y};
        return c;
        OR
        retrun x+y; */
    }

    int main()
    {  
        int a, b;
        std::cout<<"Please add both numbers.\n";
        std::cout<<"A: ";
        std::cin>>a;
        std::cout<<"B: ";
        std::cin>>b;
        
        std::cout<<addfunction(a,b);

        std::cout<<"\n\nProgram Finished.";
        
    return 0;
    }
    [\code]

    • Arman

      Mistake 1:type of addfunction function must be void.because it doesnt return any value and just print sth on screen.
      Mistake 2:you shouldn't use cout<<addfunction(a,b); because addfunction doesnt return any value.you should simply call the function itself without cout.

  • Prateek

    In second snippet(above ) :

    int add( int x , int y)            // x and y created and initialized here

    I am a bit confused here. I understand that 'x' and 'y' has been created but how is it initialized?
    Here 'x' and 'y'has not been assigned any value.

    Please do reply

  • Cade

    Under the local scope section it says: “Here’s program” instead of “Here’s a program” or “Here is a program”. Not try a be annoying just wanted to help.

    Have a Good Day:)

  • Michael Johnston

    Great explanation for people new to programming; however, as someone coming from another language where, by default, parameters are passed by reference, I got the quiz wrong. It might be helpful to include another ‘for advanced users’ snippet on this which states this explicitly.

    • Hi!

      Which language is it you're talking about? I only know languages where objects or arrays are passed by reference by default, but none where all arguments are passed by reference.

    • Dirk de Klerk

      You seemed to have skipped 2.3. He explicitly states that when arguments are passed to parameters when calling functions it is "passed by value". He even wrote it in bold.

  • Saurabh

    https://www.learncpp.com/cpp-tutorial/introduction-to-local-scope/

    This ensures variables can not be used before the point of definition (even if the compiler opts to create them before them).

    I think that this should be ... compiler opts to create them before then

    (then instead of them)

  • Juan

    The page is very good, I would just say: they can relax with the use of "astute readers". It sounds to differentiate groups of people, according to their qualities (which leads to discrimination). If this is a page to learn something without prior knowledge, it is not necessary to assume anything that has not been explained. Perhaps, instead of astute readers, less ambiguous explanations would be needed. That is why the work of rewriting parts of the tutorials, to update their content, is a good practice. Thanks for that hard work!

  • Hana

    The output of the solution's "doit" is "doIt" with a capital I instead.

  • Wilson

    First three code snippets has a small typo here:

    int z{ x + y; }

    (should be int z{ x + y };)....

    otherwise, great guide, Alex. Thanks!

  • GG

    in another example:  main‘s b and z are destroyed  should be.... main‘s b and a are destroyed

  • 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]