Local and Global Variables

Last Updated : 11 Jul, 2026

Local and global variables differ mainly in where they can be accessed and how long they exist. Choosing the appropriate type helps organize code and manage data effectively.

  • Local variables are typically used for temporary data within a function.
  • Global variables are used for data shared across multiple functions.

Local Variables

A local variable is declared inside a function or block and can only be accessed within that scope. It is created when execution enters the block and is destroyed when the block exits.

C++
#include <iostream>
using namespace std;

void exampleFunction()
{
    int x = 10;
    int y = 20;
    int sum = x + y;

    cout << "The sum is: " << sum;
}

int main()
{
    exampleFunction();
    return 0;
}
Java
public class Main {
    public static void exampleFunction() {
        // Local variable declaration
        int x = 10;
        int y = 20;
        int z = x + y;
        System.out.println("The sum is: " + z);
    }

    public static void main(String[] args) {
        exampleFunction();
    }
}
Python
def example_function():
    # Local variable declaration
    x = 10
    y = 20
    z = x + y
    print("The sum is:", z)

example_function()
C#
using System;

public class Program {
    public static void ExampleFunction() {
        // Local variable declaration
        int x = 10;
        int y = 20;
        int z = x + y;
        Console.WriteLine("The sum is: " + z);
    }

    public static void Main(string[] args) {
        ExampleFunction();
    }
}
JavaScript
function exampleFunction() {
    // Local variable declaration
    var x = 10;
    var y = 20;
    var z = x + y;
    console.log("The sum is: " + z);
}

exampleFunction();

Output
The sum is: 30

Explanation

  • x, y, and sum are local variables.
  • They are accessible only inside exampleFunction().
  • These variables are destroyed after the function finishes execution.

Advantages of Local Variables

Local variables improve code organization and reduce unintended side effects.

  • Restrict access to the current function or block.
  • Automatically release memory when the scope ends.
  • Reduce naming conflicts between different functions.
  • Improve code readability and maintainability.

Disadvantages of Local Variables

Local variables also have some limitations.

  • Cannot be accessed outside their scope.
  • Values are lost when the scope ends.
  • May hide variables with the same name in an outer scope (shadowing).

Global Variables

A global variable is declared outside all functions and can be accessed by any function in the program. It exists for the entire lifetime of the program.

C++
#include <iostream>
using namespace std;

// Global variable
int global_var = 100;

void exampleFunction()
{
    int x = 10;
    int y = 20;

    cout << "The sum is: " << x + y + global_var;
}

int main()
{
    exampleFunction();
    return 0;
}
Java
public class Main {
    // Global variable declaration
    static int global_var = 100;

    public static void exampleFunction() {
        // Local variable declaration
        int x = 10;
        int y = 20;
        int z = x + y + global_var;
        System.out.println("The sum is: " + z);
    }

    public static void main(String[] args) {
        exampleFunction();
    }
}
Python
# Global variable declaration
global_var = 100

def example_function():
    # Local variable declaration
    x = 10
    y = 20
    z = x + y + global_var
    print("The sum is:", z)

example_function()
C#
using System;

public class Program {
    // Global variable declaration
    static int global_var = 100;

    public static void ExampleFunction() {
        // Local variable declaration
        int x = 10;
        int y = 20;
        int z = x + y + global_var;
        Console.WriteLine("The sum is: " + z);
    }

    public static void Main(string[] args) {
        ExampleFunction();
    }
}
JavaScript
// Global variable declaration
var global_var = 100;

function exampleFunction() {
    // Local variable declaration
    var x = 10;
    var y = 20;
    var z = x + y + global_var;
    console.log("The sum is: " + z);
}

exampleFunction();

Output
The sum is: 130

Explanation

  • global_var is declared outside all functions.
  • It can be accessed inside exampleFunction().
  • Its value remains available until the program terminates.

Advantages of Global Variables

Global variables simplify sharing data across multiple functions.

  • Can be accessed from any function in the program.
  • Eliminate the need to pass commonly used data as function arguments.
  • Retain their values throughout program execution.
  • Useful for application-wide configuration or shared state.

Disadvantages of Global Variables

Excessive use of global variables can make programs harder to manage.

  • Any function can modify their values.
  • Increase the chances of unintended side effects.
  • Make debugging and maintenance more difficult.
  • Can lead to synchronization issues in multithreaded programs.

Difference Between Local and Global Variables

FeatureLocal VariableGlobal Variable
DeclarationInside a function or blockOutside all functions
ScopeLimited to the current blockEntire program
LifetimeExists until the block exitsExists throughout program execution
AccessibilityAccessible only within its scopeAccessible from any function
MemoryCreated when the scope beginsAllocated when the program starts
ModificationOnly within the declaring scopeCan be modified from anywhere

Related article: Scope of Variables in C++

Comment