Knowledge Card - Variable Scope
Variable Scope
-
Local Scope: Variables defined inside a function are only accessible within that function.
- Example: A variable
x
inside a function cannot be accessed outside it.
- Example: A variable
-
Global Scope: Variables defined outside functions are accessible throughout the program.
- Example: A global variable
y
can be used in any part of the program.
- Example: A global variable
-
Best Practices:
- Use local variables when possible to avoid conflicts.
- Clearly document global variables.