Skip to main content

Function Scope

Ayesha
EditReport

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:โ€‹

  1. Local scope: Variables declared inside a function are only accessible within that function.
  2. Global scope: Variables declared outside of all functions are accessible by all functions unless shadowed by a local variable of the same name.
  3. Lifetime: Local variables are created when the function is called and destroyed when it exits, whereas global variables exist throughout the program's execution.
  4. Nested scopes: Some languages (like Python) support nested function scopes, where inner functions can access variables from outer functions.
  5. 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.