Last Updated : 21 Mar, 2024
Local variables are declared within a specific block of code, such as a function or method, and have limited scope and lifetime, existing only within that block. Global variables, on the other hand, are declared outside of any function and can be accessed from any part of the program, persisting throughout its execution.
Local Variable:Local variables are variables that are declared within a specific scope, such as within a function or a block of code. These variables are only accessible within that particular scope and are typically used for temporary storage of data or for performing calculations within a limited context. Once the scope in which a local variable is defined ends, the variable typically goes out of scope and its memory is released.
In many programming languages, local variables have a limited visibility and lifespan compared to global variables, which are accessible from any part of the program. This encapsulation of variables within specific scopes helps to organize code, prevent unintended modifications, and manage memory efficiently.
Example of Local Variable:Here are the example of local variable in different language:
C++
#include <iostream>
using namespace std;
void exampleFunction() {
// Local variable declaration
int x = 10;
int y = 20;
int z = x + y;
cout << "The sum is: " << z << endl;
}
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();
}
}
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();
Python3
def example_function():
# Local variable declaration
x = 10
y = 20
z = x + y
print("The sum is:", z)
example_function()
Advantages of local variable:
Global variables are variables that are declared outside of any function or block of code and can be accessed from any part of the program. Unlike local variables, which have limited scope, global variables have a broader scope and can be used across multiple functions, modules, or files within a program. Here are some characteristics, features, advantages, disadvantages, and uses of global variables:
Example of Global Variable:Here are the example of global variable in different language:
C++
#include <iostream>
using namespace std;
// Global variable declaration
int global_var = 100;
void exampleFunction() {
// Local variable declaration
int x = 10;
int y = 20;
int z = x + y + global_var;
cout << "The sum is: " << z << endl;
}
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();
}
}
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();
Python3
# 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()
Advantages of global variable:
Local variables are declared within specific blocks of code and have limited scope, existing only within their block. Global variables, declared outside of any function, are accessible from any part of the program and persist throughout its execution. It's essential to use both judiciously, with local variables providing encapsulation and global variables offering shared data accessibility.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4