Fatskills
Practice. Master. Repeat.
Study Guide: Comp. Sci and Programming Basics: Functions and Modularity Scope (Local, Global, Enclosing, Built‑in – LEGB)
Source: https://www.fatskills.com/bsc-cs/chapter/functions-and-modularity-scope-local-global-enclosing-builtin-legb

Comp. Sci and Programming Basics: Functions and Modularity Scope (Local, Global, Enclosing, Built‑in – LEGB)

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~6 min read

Concept Summary

  • Scope in programming refers to the region of the code where a variable is defined and accessible.
  • There are four types of scope: local, global, enclosing, and built-in.
  • Local scope refers to variables defined within a function or method.
  • Global scope refers to variables defined outside of any function or method.
  • Enclosing scope refers to variables defined in an outer function or method that can be accessed by an inner function or method.

Questions


WHAT (definitional)

  1. What is the purpose of local scope in programming?
  2. Answer: Local scope is used to define variables that are only accessible within a specific function or method.
  3. Real-world example: A function that calculates the area of a rectangle might use local scope to store the width and height variables.
  4. Misconception cleared: Local scope is not the same as global scope, and variables defined in local scope are not automatically accessible in global scope.

  5. What is the difference between global and enclosing scope?

  6. Answer: Global scope refers to variables defined outside of any function or method, while enclosing scope refers to variables defined in an outer function or method that can be accessed by an inner function or method.
  7. Real-world example: A program that uses a global variable to store a user's name, and an enclosing scope to access the variable from a nested function.
  8. Misconception cleared: Enclosing scope is not the same as global scope, and variables defined in enclosing scope are not automatically accessible in global scope.

  9. What is built-in scope in programming?

  10. Answer: Built-in scope refers to variables and functions that are defined by the programming language itself, such as mathematical functions like sin() and cos().
  11. Real-world example: A program that uses the built-in math library to calculate the sine of an angle.
  12. Misconception cleared: Built-in scope is not the same as global scope, and variables defined in built-in scope are not automatically accessible in global scope.

WHY (causal reasoning)

  1. Why is it important to use local scope in programming?
  2. Answer: Using local scope helps to prevent variable name conflicts and makes code more modular and reusable.
  3. Real-world example: A program that uses local scope to store the width and height variables in a function that calculates the area of a rectangle.
  4. Misconception cleared: Local scope is not used to hide variables from other functions, but rather to define a specific region of code where variables are accessible.

  5. Why is it necessary to understand the difference between global and enclosing scope?

  6. Answer: Understanding the difference between global and enclosing scope helps to prevent variable name conflicts and makes code more modular and reusable.
  7. Real-world example: A program that uses a global variable to store a user's name, and an enclosing scope to access the variable from a nested function.
  8. Misconception cleared: Enclosing scope is not a subset of global scope, and variables defined in enclosing scope are not automatically accessible in global scope.

  9. Why are built-in scope and global scope important in programming?

  10. Answer: Built-in scope and global scope provide access to variables and functions that are defined by the programming language itself, and are used to store data and perform calculations.
  11. Real-world example: A program that uses the built-in math library to calculate the sine of an angle, and a global variable to store the user's name.
  12. Misconception cleared: Built-in scope and global scope are not the same, and variables defined in built-in scope are not automatically accessible in global scope.

HOW (process/application)

  1. How do you define a variable in local scope?
  2. Answer: A variable is defined in local scope using the var, let, or const keywords, depending on the programming language.
  3. Real-world example: A function that calculates the area of a rectangle might use the let keyword to define the width and height variables.
  4. Misconception cleared: Variables defined in local scope are not automatically accessible in global scope.

  5. How do you access a variable in enclosing scope?

  6. Answer: A variable in enclosing scope is accessed using the outer function or method's name, followed by the variable name.
  7. Real-world example: A program that uses a global variable to store a user's name, and an enclosing scope to access the variable from a nested function.
  8. Misconception cleared: Enclosing scope is not a subset of global scope, and variables defined in enclosing scope are not automatically accessible in global scope.

  9. How do you use built-in scope in programming?

  10. Answer: Built-in scope is used to access variables and functions that are defined by the programming language itself, such as mathematical functions like sin() and cos().
  11. Real-world example: A program that uses the built-in math library to calculate the sine of an angle.
  12. Misconception cleared: Built-in scope is not the same as global scope, and variables defined in built-in scope are not automatically accessible in global scope.

CAN (possibility/conditions)

  1. Can a variable be defined in both local and global scope?
  2. Answer: No, a variable can only be defined in one scope, either local or global.
  3. Real-world example: A program that uses a global variable to store a user's name, and a local variable to store a temporary value.
  4. Misconception cleared: Variables defined in local scope are not automatically accessible in global scope.

  5. Can a variable in enclosing scope be accessed from a nested function?

  6. Answer: Yes, a variable in enclosing scope can be accessed from a nested function using the outer function or method's name, followed by the variable name.
  7. Real-world example: A program that uses a global variable to store a user's name, and an enclosing scope to access the variable from a nested function.
  8. Misconception cleared: Enclosing scope is not a subset of global scope, and variables defined in enclosing scope are not automatically accessible in global scope.

  9. Can built-in scope be used to define custom functions?

  10. Answer: No, built-in scope is used to access variables and functions that are defined by the programming language itself, and cannot be used to define custom functions.
  11. Real-world example: A program that uses the built-in math library to calculate the sine of an angle.
  12. Misconception cleared: Built-in scope is not the same as global scope, and variables defined in built-in scope are not automatically accessible in global scope.

TRUE/FALSE (misconception testing)

  1. Statement: Local scope is used to hide variables from other functions.
  2. Answer: FALSE
  3. Real-world example: A program that uses local scope to store the width and height variables in a function that calculates the area of a rectangle.
  4. Misconception cleared: Local scope is used to define a specific region of code where variables are accessible, not to hide variables from other functions.

  5. Statement: Enclosing scope is a subset of global scope.

  6. Answer: FALSE
  7. Real-world example: A program that uses a global variable to store a user's name, and an enclosing scope to access the variable from a nested function.
  8. Misconception cleared: Enclosing scope is not a subset of global scope, and variables defined in enclosing scope are not automatically accessible in global scope.

  9. Statement: Built-in scope is the same as global scope.

  10. Answer: FALSE
  11. Real-world example: A program that uses the built-in math library to calculate the sine of an angle, and a global variable to store the user's name.
  12. Misconception cleared: Built-in scope is used to access variables and functions that are defined by the programming language itself, and is not the same as global scope.


ADVERTISEMENT