Variables and Fundamental Types

Local and Global Variables

There are several scopes where we can create an object (declare a variable), and the location of this declaration determines when its name can be referenced and how long it may live. Two common scope definitions include local and global variables.

A local object may be created either inside a function or inside a block of code delimited by curly braces {}. The name of this object may be used only inside the function and the duration of the object is generally limited to the execution scope where its name may be used.

When its scope exits, it gets destroyed and the object's name becomes inaccessible.

#include <iostream>

void greet()
{
    int times{3};

    for (int i{0}; i < times; ++i)
    {
        std::cout << "Hello\n";
    }
}

int main()
{
    greet();

    // std::cout << times << '\n';
}

Such local variables generally keep the data as close as possible to the code which uses it.

A global object may be declared before any of the functions. The scope of this object lasts up to the end of the program and the functions written after its declaration may use its name.

#include <iostream>

int counter{0};

void increment()
{
    // Increment the value before the expression uses it
    ++counter;
}

void show()
{
    std::cout << "Counter = "
              << counter
              << '\n';
}

int main()
{
    show();

    increment();
    increment();

    show();
}

Since the same object may be accessible to many functions, then any function, with the scope for it, can view and modify it. This is convenient but may lead to some hidden dependencies between different parts of the program.

#include <iostream>

int global_value{100};

void example()
{
    int local_value{50};

    std::cout << "Local:  "
              << local_value
              << '\n';

    std::cout << "Global: "
              << global_value
              << '\n';
}

int main()
{
    example();

    // std::cout << local_value << '\n';

    std::cout << "Global from main: "
              << global_value
              << '\n';
}
  • Local object is created when the execution enters the block or function where it is created and gets destroyed when it exits that region.

  • Global object lives for the duration of the program.

The visibility and lifespan difference makes them differ from each other.

When numerous functions use the global variable, it gets difficult to understand the state of the program at any given moment, as a seemingly independent piece of code may end up modifying its state after some other function invoked it.

I hardly use new global variables in my modern programming. Instead, I prefer passing values in function parameters or by reference and receiving values from function return values, which is more transparent and testable.

Global objects, however, can still be encountered and used in legacy systems and specific kinds of applications, though they should be used with the best possible care.

0 comments

No comments yet. Be the first.

Add a comment

About CppLearn

CppLearn is an interactive, free reference for modern C++. In each lesson, you'll find out how the language works, rather than just lines of code that you can copy and paste. We cover concepts starting from beginner lessons on fundamentals and proceed to templates, move semantics, smart pointers, and areas that usually trap or scare C++ learners.