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.
#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;
}
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();
}
}
def example_function():
# Local variable declaration
x = 10
y = 20
z = x + y
print("The sum is:", z)
example_function()
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();
}
}
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.
#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;
}
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();
}
}
# 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()
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();
}
}
// 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
| Feature | Local Variable | Global Variable |
|---|---|---|
| Declaration | Inside a function or block | Outside all functions |
| Scope | Limited to the current block | Entire program |
| Lifetime | Exists until the block exits | Exists throughout program execution |
| Accessibility | Accessible only within its scope | Accessible from any function |
| Memory | Created when the scope begins | Allocated when the program starts |
| Modification | Only within the declaring scope | Can be modified from anywhere |
Related article: Scope of Variables in C++