Variable Scope and Modular Programming

Chapter 3 · Tamil Nadu State Board

  • 7 min read
  • 6-question quiz

Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content

In short

This lesson explains how scope determines where program names and variables can be used. It teaches the LEGB search order and describes local, global, enclosed, and built-in scopes. It also explains how modules divide programs into manageable parts and how access control limits the use of class members. These ideas help programmers organize code, reuse it, and control access to data.

Written by AI from this lesson. How we check it

Key points

  • A variable's scope is the part of a program where that variable is visible.
    From your textbook
    “The scope of a variable is that part of the code where it is visible.”
  • Mapping connects a variable name with an object, and namespaces keep track of these connections.
    From your textbook
    “Namespaces are containers for mapping names of variables to objects.”
  • LEGB sets the order for searching scopes: Local, Enclosed, Global, then Built-in.
    From your textbook
    “The LEGB rule is used to decide the order in which the scopes are to be searched for scope resolution.”
  • A local variable is defined in the current function, while a global variable is declared outside all functions.
    From your textbook
    “A variable which is declared outside of all the functions in a program is known as global variable.”
  • A module is a program part, and modular programming divides a program into separate sub-programs.
    From your textbook
    “The process of subdividing a computer program into separate sub-programs is called Modular programming.”
  • Access control restricts who or what can use resources; private, public, and protected members have different access.
    From your textbook
    “Private members of a class are denied access from the outside the class.”
Test yourself: 6 questions

Variable Scope, Mapping, and Lifetime

Definition

Scope
Scope is the part of a program where a variable can be accessed.

A variable name is mapped to an object in memory. A namespace keeps track of these name-to-object mappings. More than one variable name can map to the same object.

Worked example

What happens after these statements: a:=5, b:=a, a:=3?

Try it first, then show the solution
  1. First, a maps to the integer value 5.
  2. Next, b maps to the same integer value as a: 5.
  3. Then a maps to 3. Changing a does not change the value mapped to b.

Answer: a is mapped to 3, while b is still mapped to 5.

Definition

Lifetime
The lifetime of a variable is the duration for which it remains alive.

A variable defined inside a procedure may be available only until that procedure ends. Trying to use it outside its scope can produce the error “name ‘a’ is not defined”.

LEGB Rule for Finding Variables

When a program needs a variable, the LEGB rule gives the order for searching scopes. The search moves from the most local scope outward.

LEGB search order

  • Local (L): defined inside the current function or class.
  • Enclosed (E): defined inside an enclosing function.
  • Global (G): defined at the uppermost level of the program.
  • Built-in (B): reserved names available through built-in functions or modules.

How the scopes differ

ScopeWhere the variable or name is found
LocalInside the current function.
EnclosedInside a function that contains the current function.
GlobalOutside all functions, at the uppermost program level.
Built-inAmong names provided through the programming language's built-in functions or modules.

A function checks its local scope first. If the name is not found there, the search continues through enclosing scopes, then global scope, and finally built-in scope.

Remember: Remember the search order: Local, Enclosed, Global, Built-in.

Local, Global, Enclosed, and Built-in Scope

Definition

Local scope
Local scope contains variables defined in the current function.

A function looks for a name locally before checking outer scopes. A variable available in the function's local scope can be used there.

Definition

Global scope
Global scope contains variables declared outside all functions in a program.

A global variable can be accessed inside or outside functions. A local variable with the same name can have a different value from the global variable.

Definition

Enclosed scope
Enclosed scope contains variables in an outer function that can be accessed by a function nested inside it.

A function defined inside another function is a nested function. The inner function can use a variable belonging to its enclosing function.

Definition

Built-in scope
Built-in scope contains names made available through the programming language's built-in functions or modules.

Names from library files become available when those files are imported. Functions and modules supplied with the software are included in this scope.

Modules and Modular Programming

Definition

Module
A module is a part of a program that contains one or more closely related statements.

A program can contain independently developed modules. Each module can work on its own and can also be combined with other modules.

Definition

Modular programming
Modular programming is the process of dividing a computer program into separate sub-programs.

Why programs are divided into modules

  • It makes programming and debugging easier.
  • Programmers can divide the work.
  • Pieces of the program can be debugged independently.
  • Small functional modules work together to produce the output.

Examples of modules

  • Procedures
  • Subroutines
  • Functions

Variable Scope, Mapping and Namespaces

Definition

Scope
Scope is the part of a program where a variable, parameter or function is visible.

Definition

Mapping
Mapping is the process of binding a variable name to an object, using = in programming languages.

Definition

Namespaces
Namespaces are containers that map variable names to objects.

The LEGB order for searching scopes

  • Local scope: variables defined in the current function.
  • Enclosed scope: variables in an outer function that an inner function can access.
  • Global scope: variables declared outside all functions in a program.
  • Built-in scope: names already loaded into the program when the compiler or interpreter starts.

Remember: LEGB gives the order used to search scopes when resolving a name.

Modules and Modular Programming

Definition

Module
A module is a part of a program, and a program can contain one or more independently developed modules.

Definition

Modular programming
Modular programming is the process of dividing a computer program into separate sub-programs.

Characteristics of modules

  • They contain instructions, processing logic and data.
  • They can be compiled separately and stored in a library.
  • They can be included in a program.
  • Their segments can be used by invoking a name and parameters.
  • Their segments can be used by other modules.

Benefits of modular programming

  • Less code needs to be written.
  • A procedure can be reused, so its code does not need to be typed repeatedly.
  • Small teams can work on smaller parts of a program, making design easier.
  • Many programmers can collaborate on the same application.
  • Code can be stored across multiple files.
  • Code is short, simple and easy to understand.
  • Errors are easier to find because they are localized to a subroutine or function.
  • The same code can be used in many applications.
  • Variable scope can be controlled easily.

Access Control and Class Members

Definition

Access control
Access control is a security technique that regulates who or what can view or use resources in a computing environment.

Access control selectively restricts access to data and helps minimize risk to an object. In object oriented programming languages, access modifiers control access to class members. C++ and Java use public, private and protected keywords.

Class member access

Member typeWho can access it
PrivateOnly code within the class; access from outside the class is denied.
PublicCode outside the class can access it. The object of the same class is required to invoke a public method.
ProtectedCode within the class and its sub-classes can access it.

Private instance variables and public methods together support data encapsulation. Protected members let a child class inherit specific resources from its parent class.

Default member access in Python, C++ and Java

LanguageDefault access and restrictions
PythonAll class members are public by default. Python uses a single or double underscore prefix as a convention to imitate protected and private access.
C++ and JavaMembers are private by default. Public, private and protected keywords control access.

Remember: Private members are handled only within their class, while protected members are also available to sub-classes.

Questions students ask

Short, direct answers to what students search for in this chapter.

What is scope in programming?

Scope is the part of a program where a variable can be accessed. A variable may be available in one part of a program but not in another. Its scope helps determine where the program can use its name.

What is the LEGB rule?

The LEGB rule gives the order for searching for a variable name: Local, Enclosed, Global, and Built-in. The program checks the current function first, then enclosing functions, then the uppermost program level, and finally built-in names.

What is the difference between local and global scope?

Local scope contains variables defined in the current function. Global scope contains variables declared outside all functions. A global variable can be accessed inside or outside functions, while a local variable belongs to its function's scope.

What is enclosed scope in programming?

Enclosed scope contains a variable declared in an outer function when that function contains another function. The inner, nested function can access the outer function's variable. In the LEGB search order, enclosed scope is checked after local scope.

What is modular programming?

Modular programming is the process of dividing a computer program into separate sub-programs called modules. Modules can be developed and debugged independently, then combined to work together. Procedures, subroutines, and functions are examples of modules.

What is modular programming and why is it useful?

Modular programming divides a computer program into separate sub-programs. It can reduce repeated code, make code shorter and easier to understand, and help teams work on different parts. It also makes errors easier to locate and allows code to be reused in many applications.

Show 2 more questions

What is the difference between public, private and protected members?

Public members can be accessed from outside the class. Private members can be handled only within the class. Protected members can be accessed within the class and by its sub-classes. C++ and Java use keywords to control these access types.

How does Python represent private and protected access?

Python does not effectively restrict access to an instance variable or method. It uses a naming convention: a single or double underscore prefix can imitate protected or private access. All members in a Python class are public by default.

Book back answers

20 textbook exercises, solved and checked against the book. Questions we could not prove are left out.

Choose the best answer (1 Mark)

  1. Which of the following refers to the visibility of variables in one part of a program to another part of the same program.

    • (A) Scope
    • (B) Memory
    • (C) Address
    • (D) Accessibility
    Try it, then show the answer

    (A) Scope

    Scope refers to the visibility of a variable from one part of a program to another.

    From the book: “Scope refers to the accessibity of a variable with in one part of a program to another part of the same program.”

  2. The process of binding a variable name with an object is called

    • (A) Scope
    • (B) Mapping
    • (C) late binding
    • (D) early binding
    Try it, then show the answer

    (B) Mapping

    The chapter calls binding a variable name with an object mapping.

    From the book: “The process of binding a variable name with an object is called mapping.”

  3. Which of the following is used in programming languages to map the variable and object?

    • (A) ::
    • (B) :=
    • (C) =
    • (D) ==
    Try it, then show the answer

    (C) =

    The chapter says the equal to sign is used to map a variable and object.

    From the book: “= (equal to sign) is used in programming languages to map the variable and object.”

  4. Containers for mapping names of variables to objects is called

    • (A) Scope
    • (B) Mapping
    • (C) Binding
    • (D) Namespaces
    Try it, then show the answer

    (D) Namespaces

    Namespaces are the containers that map variable names to objects.

    From the book: “Namespaces are containers for mapping names of variables to objects.”

All 20 book back answers →

Practice

Question 1 of 6Score 0

What does scope describe?

Exam questions

Practice questions with model answers. These are not from an official paper.

What is scope, and what does the LEGB rule determine?2 marks

Scope is the part of the code where a variable is visible. The LEGB rule determines the order in which scopes are searched: Local, Enclosed, Global, and Built-in.

Explain the difference between local and global scope.2 marks

Local scope refers to variables defined in the current function. A global variable is declared outside all functions and can be accessed inside or outside them.

What is modular programming, and give two benefits.2 marks

Modular programming divides a computer program into separate sub-programs. It makes code easier to understand and helps programmers identify errors because they are localized to a subroutine or function.

Compare public, private, and protected members.5 marks

Access control regulates who can view or use resources in a computing environment. In object-oriented languages, access modifiers control access to class members.

  1. Public: Public members are accessible from outside the class. An object of the same class is required to invoke a public method.
  2. Private: Private members cannot be accessed from outside the class and can be handled only within the class.
  3. Protected: Protected members can be accessed within the class and by its subclasses. Other processes are not permitted to access them.
  4. Purpose of protected access: Protected access allows specific resources of a parent class to be inherited by a child class.
  5. Access modifiers in C++ and Java: C++ and Java use the public, private and protected keywords to control access to class members.
  6. Python convention: Python does not effectively restrict access to instance variables or methods. It uses single or double underscores as a convention to imitate protected and private access.

Thus, public members are accessible outside the class, private members are restricted to the class, and protected members are accessible to the class and its subclasses.

Watch out

  • Do not assume that changing one variable changes another variable mapped to the same earlier value; the lesson's example shows b remains mapped to 5 when a is changed to 3.
  • Do not confuse protected with private: protected members are also available to sub-classes, while private members are handled only within the class.

Still stuck on this lesson?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →