Scope of a variable in different programming languages
1. Scope of a variable in C
Variables in C can have two types of scopes:
- Local Scope or Block Scope: The variables declared within the local scope are called local variables. Local variables are visible in the block they are declared in and other blocks nested inside that block.
- Global Scope or File Scope: The variables declared in the global scope are called global variables. Global variables are visible in every part of the program.
Below is the implementation of different types of Scopes in C:
#include <stdio.h>
// Global variable
int global_var = 5;
void printValues()
{
// Local variable
int local_var = 10;
printf("Inside function: global_var = %d, local_var = "
"%d\n",
global_var, local_var);
}
int main()
{
printf("In main: global_var = %d\n", global_var);
printValues();
return 0;
}
Output
In main: global_var = 5 Inside function: global_var = 5, local_var = 10
2. Scope of a variable in C++
Similar to C, Variables in C++ can have two types of scopes:
- Local Scope or Block Scope: The variables declared within the local scope are called local variables. Local variables are visible in the block they are declared in and other blocks nested inside that block.
- Global Scope or File Scope: The variables declared in the global scope are called global variables. Global variables are visible in every part of the program.
Below is the implementation of different types of Scopes in C++:
#include <iostream>
using namespace std;
int global_var = 10; // Global variable
void printValues() {
int local_var = 20; // Local variable
{
int block_var = 30; // Block variable
cout << "Inside block: local_var = " << local_var << ", block_var = " << block_var << endl;
}
cout << "Inside function: local_var = " << local_var << endl;
}
int main() {
cout << "In main: global_var = " << global_var << endl;
printValues();
return 0;
}
Output
In main: global_var = 10 Inside block: local_var = 20, block_var = 30 Inside function: local_var = 20
3. Scope of a variable in Java
Variables in Java can have three types of scopes:
- Local Scope or Block Scope: The variables declared within the local scope are called local variables. Local variables are visible in the block they are declared in and other blocks nested inside that block.
- Class Scope: The variables declared in the class with private access modifier but outside the methods, have class scope. Class Scoped variables can be used anywhere inside the class but not outside it.
- Block Scope: The variables declared inside a block, wrapped in a pair of curly braces {} have block scope. Block scoped variables can be accessed anywhere inside the block but not outside it.
Below is the implementation of different types of Scopes in Java:
public class MyClass {
int class_var = 2; // Class variable
public void printValues() {
int local_var = 4; // Local variable
System.out.println("Inside function: class_var = " + class_var + ", local_var = " + local_var);
}
public static void main(String[] args) {
MyClass obj = new MyClass();
System.out.println("In main: class_var = " + obj.class_var);
{
int block_var = 6; // Block variable
System.out.println("In block: block_var = " + block_var);
}
obj.printValues();
}
}
Output
In main: class_var = 2 In block: block_var = 6 Inside function: class_var = 2, local_var = 4
4. Scope of a variable in Python
Variables in Java can have three types of scopes:
- Local Scope: In Python, variables are usually limited to where they are created, such as inside functions, loops, or blocks of code. These are called local variables.
- Global Scope: Global variables are the ones that are defined and declared outside any function and are not specified to any function. They can be used by any part of the program.
Below is the implementation of different types of Scopes in Python:
def fnc():
# Local Variable
local_var = 5
print("Local Variable: ", local_var)
# Accessing global variable inside the function
global global_var
print("Global Variable inside function: ", global_var)
# Global Variable
global_var = 10
print("Global Variable: ", global_var)
fnc()
Output
Global Variable: 10 Local Variable: 5 Global Variable inside function: 10
5. Scope of a variable in C#
Variables in C# can have two types of scopes:
- Local Scope or Block Scope: The variables declared within the local scope are called local variables. Local variables are visible in the block they are declared in and other blocks nested inside that block.
- Class Scope: The variables declared inside the class but outside any method are called class variables.
Below is the implementation of different types of Scopes in C#:
using System;
class MyClass {
int class_var = 10; // Class variable
void printValues() {
int local_var = 20; // Local variable
Console.WriteLine("Inside function: class_var = {0}, local_var = {1}", class_var, local_var);
}
static void Main(string[] args) {
MyClass obj = new MyClass();
obj.printValues();
}
}
Output
Inside function: class_var = 10, local_var = 20
6. Scope of a variable in JavaScript
Variables in JavaScript can have two types of scopes:
- Block Scope: Block scope was introduced in ES6(2015). The variables declared inside a block, wrapped in a pair of curly braces {} have block scope. Block scoped variables can be accessed anywhere inside the block but not outside it. let and const are block scoped whereas var is global scoped.
- Global Scope: The variables declared outside any function or block have Global scope.
- Function Scope: The variables declared inside any function is called function scoped. In Javascript, each function creates a new scope. The variables declared inside a function (var, let or const) cannot be accessed outside the function.
Below is the implementation of different types of Scopes in JavaScript:
function outerFunction() {
let x = 10; // Function Scoped
if (true) {
let y = 20; // Block Scoped
console.log("Inside block scope:", x, y);
}
console.log("Inside function:", x);
}
outerFunction();
let z = 5;
console.log("Global scope: ", z);
Output
Inside block scope: 10 20 Inside function: 10 Global scope: 5
Scope of a variable
Scope of a variable defines the part of code where the variable can be accessed or modified. It helps in organizing code by limiting where variables are available, preventing unintended changes or conflicts. Understanding scope is key to writing clear, efficient, and maintainable programs. In this article, we will cover the basics of Scope of a variable, types of Scope in different languages, its importance, etc.