Function Scope
Overview
Function scope refers to the accessibility or visibility of variables within a function. Variables declared inside a function are local to that function, meaning they cannot be accessed outside of it. Similarly, variables outside the function are generally global and can be accessed by the function depending on the language's scope rules.
Syntax
C++
// Local scope example
void function_name() {
int local_variable = 10; // This variable is only accessible inside the function
}
Video Explanation

Example
C++ Example
// Global and local scope
int global_var = 20;
void display() {
int local_var = 10;
std::cout << "Local Variable: " << local_var << std::endl; // Accessible
std::cout << "Global Variable: " << global_var << std::endl; // Accessible
}
std::cout << local_var; // Error: local_var is not accessible here
Syntax
Python
# Local scope example
def function_name():
local_variable = 10 # This variable is only accessible inside the function
Video Explanation

Example
Python Example
# Global and local scope
global_var = 20
def display():
local_var = 10
print(f"Local Variable: {local_var}") # Accessible
print(f"Global Variable: {global_var}") # Accessible
print(local_var) # Error: local_var is not accessible here
Key Points:
- Local scope: Variables declared inside a function are only accessible within that function.
- Global scope: Variables declared outside of all functions are accessible by all functions unless shadowed by a local variable of the same name.
- Lifetime: Local variables are created when the function is called and destroyed when it exits, whereas global variables exist throughout the program's execution.
- Nested scopes: Some languages (like Python) support nested function scopes, where inner functions can access variables from outer functions.
- Scope resolution: In C++, the :: operator allows access to global variables when a local variable shadows a global one.
Track Your Progress
Done with this topic? Mark it as complete to track your progress.
💬 Discuss this page
Have a question or spot something confusing in "Function Scope"? Ask below — it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.