Knowledge Card - Variable Scope

Variable Scope

  1. 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.
  2. 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.
  3. Best Practices:

    • Use local variables when possible to avoid conflicts.
    • Clearly document global variables.