Chapter 5_Names, Bindings, and Scope

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/98

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 5:49 AM on 4/21/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

99 Terms

1
New cards

Imperative programming languages

  • are, to varying degrees, abstractions of the underlying von Neumann computer architecture.

  • are designed in a way that closely follows how a computer actually works. They tell the computer step by step what to do, just like a set of instructions in a recipe.

  • is about giving the computer a sequence of commands to change its memory and perform tasks.

  • Focus

    • How to do something

  • State Changes

    • Uses variables that change

  • Examples

    • C, Java, Python (loops)

  • Code Style

    • Step-by-step instructions

  • Uses variables to store values and update them as needed.

  • Uses control structures such as loops (for, while) and conditional statements (if-else).

  • Focuses on changing the program’s state (i.e., modifying values in memory).

2
New cards

von Neumann architecture

is the basic design of most computers.

3
New cards

memory,

processor

von Neumann architecture’s primary components (2)

4
New cards

memory

stores both the instructions (commands) and the data (information) needed for a program.

5
New cards

processor

provides operations for modifying the contents of the memory

6
New cards

Functional programming languages

  • allow expressions to be named.

  • However, there is a key difference: once you assign a value to a name in functional programming, it cannot be changed.

    • This makes it more like a constant in imperative languages (where constants are values that don’t change).

  • Uses functions that avoid changing variables (e.g., Haskell, Lisp).

  • Focus

  • What to do

  • State Changes

    • Avoids changing variables

  • Examples

    • Haskell, SQL, Prolog

  • Code Style

    • Expresses logic or computations without steps

7
New cards

pure functional programming languages

  • do not have variables that are like those of the imperative languages.

    • However, many _____ do include such variables.

  • there are no traditional variables that store changing values.

    • But some functional languages do allow variables that behave more like those in imperative languages.

8
New cards

Non-imperative programming

  • is like stating what you want, without specifying how to do it.

  • The computer figures out the process itself.

9
New cards

Functional Programming,

Logic Programming,

Query-Based Programming

Non-imperative programming (3)

10
New cards

Logic Programming

Uses rules and facts to infer conclusions (e.g., Prolog).

11
New cards

Query-Based Programming

Focuses on retrieving data without specifying how (e.g., SQL).

12
New cards

Are names case sensitive?

Are the special words of the language reserved words or keywords?

Design Issues (2)

13
New cards

case sensitivity and reserved words

  • impact code readability, ease of use, error prevention, and compatibility.

  • These are considered issues because they can create challenges for programmers and influence how code is written and understood.

14
New cards

name

  • is a string of characters used to identify some entity in a program.

  • is technically called an identifier.

  • in most programming languages have the same form: a letter followed by a string consisting of letters, digits, and underscore characters ( _ ).

15
New cards

Fortran 95+

allows up to 31 characters in its names.

16
New cards

C99

has no length limitation on its internal names, but only the first 63 are significant.

17
New cards

Java,

C#,

Ada

Names in _____, _____, and _____ have no length limit, and all characters in them are significant.

18
New cards

C++

does not specify a length limit on names, although implementors sometimes do.

19
New cards

underscore

Although the use of _____ characters to form names was widely used in the 1970s and 1980s, that practice is now far less popular.

20
New cards

C-based,

camel notation

In the _____ languages, it has to a large extent been replaced by the so-called _____, in which all of the words of a multiple-word name except the first are capitalized, as in myStack.

21
New cards

programming style issue

Note that the use of underscores and mixed case in names is a _____, not a language design issue.

22
New cards

PHP

All variable names in _____ must begin with a dollar sign.

23
New cards

Perl

In _____, the special character at the beginning of a variable’s name, $, @, or %, specifies its type (although in a different sense than in other languages).

24
New cards

Ruby

In _____, special characters at the beginning of a variable’s name, @ or @@, indicate that the variable is an instance or a class variable, respectively.

25
New cards

C-based languages

  • In many languages, notably the _____, uppercase and lowercase letters in names are distinct; that is, names in these languages are case sensitive.

  • allow any compound statement (a statement sequence surrounded by matched braces) to have declarations and thereby defined a new scope.

26
New cards

case sensitivity

  • To some people, this is a serious detriment to readability, because names that look very similar in fact denote different entities.

  • In that sense, _____ violates the design principle that language constructs that look similar should have similar meanings. But in languages whose variable names are case-sensitive, although Rose and rose look similar, there is no connection between them.

27
New cards

Hard to Read

Names that look almost the same might mean totally different things.

28
New cards

Easy to Make Mistakes

A small typo can cause errors that are hard to find.

29
New cards

Reserved Words

  • Cannot be used as variable or function names.

  • In most languages, special words are classified as ______, which means they cannot be redefined by programmers

  • are better than keywords because the ability to redefine keywords can be confusing.

30
New cards

Keywords

  • Can sometimes be used as variable names if the context allows

  • is a word of a programming language that is special only in certain contexts.

31
New cards

Reserved Keywords

are words that have a special, predefined meaning.

32
New cards

Fortran or Formula Translation

  • is the only remaining widely used language whose special words are keywords.

  • is a "grandfather" of programming languages, and it handles this very differently.

  • words are often keywords, not reserved words.

33
New cards

Integer

  • In Fortran, the word _____, when found at the beginning of a statement and followed by a name, is considered a keyword that indicates the statement is a declarative statement.

  • However, if the word _____ is followed by the assignment operator, it is considered a variable name.

34
New cards

variable

  • is an abstraction of a computer memory cell or collection of cells

  • characterized by a collection of properties, or attributes,

  • are like labels for specific storage locations in a computer's memory.

  • They act as an abstraction, meaning they simplify how we interact with memory without dealing with its complexity.

35
New cards

name,

address,

value,

type,

lifetime,

scope

A variable can be characterized as a sextuple of attributes (6)

36
New cards

Address

  • is the machine memory address with which it is associated.

  • The actual location in memory where the variable's data is stored.

  • It is possible to have multiple variables that have the same _____

37
New cards

aliases

When more than one variable name can be used to access the same memory location, the variables are called _____

38
New cards

Aliasing

is a hindrance to readability because it allows a variable to have its value changed by an assignment to a different variable.

39
New cards

type

  • determines the range of values the variable can store and the set of operations that are defined for values of the type.

  • The kind of data the variable holds (e.g., integer, string, boolean).

  • a fundamental concept in programming languages.

  • determines how the computer interprets and uses the data.

40
New cards

Value

  • is the contents of the memory cell or cells associated with the variable.

  • The current data stored in the variable (e.g., age = 25).

41
New cards

abstract memory cell

has the size required by the variable with which it is associated.

42
New cards

binding

is an association between an attribute and an entity, such as between a variable and its type or value, or between an operation and a symbol.

43
New cards

binding time

time at which a binding takes place

44
New cards

language design time,

language implementation time,

compile time,

load time,

link time,

run time

Bindings can take place (binding time) at (6)

45
New cards

Language Design Time

When the rules of the language are created (e.g., deciding that int means integer).

46
New cards

Language Implementation Time

When compilers or interpreters are built to follow the language rules.

47
New cards

Compile Time

When the program is translated into machine code (e.g., checking variable types)

48
New cards

Load Time

When the program is loaded into memory before execution.

49
New cards

Link Time

When different parts of a program are combined (e.g., connecting external libraries).

50
New cards

Run Time

When the program is actually running (e.g., assigning a new value to a variable).

51
New cards

Identifier (The Name),

Entity (The Thing)

Parts of a Binding (2)

52
New cards

Identifier (The Name)

This is the word you type in your code (e.g., user_age).

53
New cards

Entity (The Thing)

This is the actual data, memory address, or function the name represents.

54
New cards

Different bindings (associations)

_____ happen at different times when the program is being written, compiled, and executed.

55
New cards

Static Binding,

Dynamic Binding

Binding Types (2)

56
New cards

Static Binding

  • This happens before the program even runs (usually while the code is being compiled).

  • It is permanent and fast.

57
New cards

Dynamic Binding

  • This happens while the program is running.

  • The name can be linked to different things at different times.

  • the type of a variable is not specified by a declaration statement, nor can it be determined by the spelling of its name.

  • the variable is bound to a type when it is assigned a value in an assignment statement.

58
New cards

explicit declaration

  • is a statement in a program that lists variable names and specifies that they are a particular type.

  • make code clearer and safer, preventing mistakes.

  • This means you clearly specify a variable’s type when declaring it.

59
New cards

Implicit declarations

  • is a means of associating variables with types through default conventions, rather than declaration statements.

  • make coding faster and more flexible, but may introduce errors if the wrong type is assumed

  • This means the compiler automatically assigns a type based on how the variable is used.

  • Some of the problems here can be avoided by requiring names for specific types to begin with particular special characters.

  • mean that a variable’s type is not explicitly stated when it is first used.

60
New cards

names of variables,

data itself

  • In reality, the _____ are never bound to types.

  • Names can be bound to variables and variables can be bound to types.

  • Instead, a variable name is connected to a memory location (where the data is stored).

  • The _____ determines the type of the variable at any given time.

61
New cards

allocation

  • The process where a memory cell to which a variable is bound somehow must be taken from a pool of available memory.

  • is the technical term for reserving memory for a variable.

  • The operating system or programming language runtime manages memory _____ automatically or manually (depending on the language).

62
New cards

Deallocation

  • is the process of placing a memory cell that has been unbound from a variable back into the pool of available memory.

  • When a variable is no longer needed, its memory should be released so that other parts of the program can use it.

63
New cards

lifetime

  • is the time during which the variable is bound to a specific memory location.

  • begins when it is bound to a specific cell and ends when it is unbound from that cell.

  • In a Java method, it is the period of time beginning when the method is entered and ending when execution of the method terminates

    • The variable comes into existence when the method starts.

    • It disappears from memory when the method finishes execution

64
New cards

Static variables

are those that are bound to memory cells before program execution begins and remain bound to those same memory cells until program execution terminates.

65
New cards

scope

  • is the range of statements in which the variable is visible.

    • A variable is visible in a statement if it can be referenced in that statement.

  • In a Java method, it is from its declaration to the end of the method

66
New cards

Local variable

is local in a program unit or block if it is declared there.

67
New cards

Non-local variable

  • of a program unit or block are those that are visible within the program unit or block but are not declared there.

  • Usually, they are global variables (declared outside functions).

68
New cards

ALGOL 60

introduced the method of binding names to non-local vars is called static scoping.

69
New cards

Static scoping

  • method of binding names to non-local vars

  • is named because the scope of a variable can be statically determined – that is prior to execution.

  • This permits a human program reader (and a compiler) to determine the type of every variable in the program simply by examining its source code.

  • Python mainly uses static scoping, meaning a variable’s scope is determined by its location in the code, not by which functions are currently running.

70
New cards

Nested Subprograms,

Subprograms that cannot be nested

categories of static scoped languages (2)

71
New cards

Nested Subprograms (The "Matryoshka Doll" Style)

  • Functions (or procedures) can be defined inside other functions.

  • Inner functions have access to the variables of their outer functions.

72
New cards

Subprograms That Cannot Be Nested (The "Apartment Building" Style)

  • Functions cannot be defined inside other functions.

  • Each function is declared separately and does not automatically access variables from other functions.

73
New cards

ALGOL 60

allows a section of code to have its own local variables whose scope is minimized.

74
New cards

stack dynamic

  • From ALGOL 60, allows a section of code to have its own local variables whose scope is minimized.

  • Such variables are _____, so they have their storage allocated when the section is entered and deallocated when the section is exited.

75
New cards

C99,

C++,

Java,

C#

allow variable declarations to appear anywhere a statement can appear (4)

76
New cards

C99,

C++,

Java

the scope of all local variables is from the declaration to the end of the block (3)

77
New cards

C#

  • the scope of any variable declared in a block is the whole block, regardless of the position of the declaration in the block

  • However, a variable still must be declared before it can be used

78
New cards

C++,

Java,

C#

  • variables can be declared in for statements (3)

  • The scope of such variables is restricted to the for construct

79
New cards

C,

C++,

PHP,

Python

  • support a program structure that consists of a sequence of function definitions in a file (4)

  • These languages allow variable declarations to appear outside function definitions

80
New cards

Dynamic scoping

  • is a method of determining which variables a function can access based on the order of function calls at runtime, rather than where the variables are written in the code.

  • looks at the most recent functions that were called and allows the current function to use variables from those functions, even if the variables were declared outside the current function.

  • When a variable is used in _____, the program searches backward through all active function calls to find where it was last declared.

    • This means that a function can use variables from functions that called it, even if those variables were never passed as parameters.

    • The program does not check the written structure but instead looks at which functions are currently running.

  • the referencing environment is the local variables plus all visible variables in all active subprograms."

81
New cards

APL,

SNOBOL4,

early versions of LISP

The scope of variables in _____, _____, and the _____ is dynamic.

82
New cards

Perl,

Common Lisp

_____ and _____ allow variables to be declared to have dynamic scope, although the default scoping mechanism is these languages is static.

83
New cards

References to variables

are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point.

84
New cards

static variable in a C or C++ function

  • Statically bound to the scope of that function and is also statically bound to storage

    • The scope of a static variable is limited to the function where it is declared.

    • However, it is stored permanently (not created and destroyed with function calls).

  • Its scope is static and local to the function but its lifetime extends over the entire execution of the program of which it is a part

    • variable can only be accessed within its function (local scope).

    • However, it stays in memory for the entire runtime of the program.

    • This means even if the function is called multiple times, the variable retains its value from previous calls.

85
New cards

referencing environment

  • of a statement is the collection of all names that are visible in the statement

  • of a statement is needed while that statement is being compiled,

    • so code and data structures can be created to allow references to variables from other scopes during run time.

  • refers to the set of variables that are visible and accessible at a particular point in a program.

  • It defines where and how a variable can be accessed within a program's structure.

86
New cards

variables,

data structures,

during execution

Why is the Referencing Environment Needed During Compilation?

  • When the compiler reads the program, it needs to determine which _____ a statement can access.

  • The compiler uses this information to set up _____ (such as symbol tables and memory locations) that will allow the program to correctly reference variables at runtime.

  • This ensures that, _____, the program can retrieve the correct variable value, even if the variable is declared in another scope.

87
New cards

subprogram

  • is active if its execution has begun but has not yet terminated.

  • While it is active, it can still use and modify variables.

88
New cards

Local (L),

Enclosing (E),

Global (G),

Built-in (B)

In Python, the LEGB Rule determines the referencing environment

89
New cards

Local (L)

Variables defined inside a function.

90
New cards

Enclosing (E)

Variables in an outer function when using nested functions.

91
New cards

Global (G)

Variables defined at the top level of the script.

92
New cards

Built-in (B)

Predefined Python functions and constants (like print() or len()).

93
New cards

During Compilation

How Referencing Environment is Used During Compilation and Runtime

  • The Python interpreter analyzes which variables are available for each statement.

94
New cards

During Runtime

How Referencing Environment is Used During Compilation and Runtime

  • When inner() executes, Python follows the LEGB rule to find variable values dynamically.

95
New cards

Named Constants

It is a variable that is bound to a value only at the time it is bound to storage; its value cannot be change by assignment or by an input statement.

96
New cards

Readability,

Modifiability

Named Constants Advantages (2)

97
New cards

Readability

Instead of using the number 100 everywhere in your code, you can use LEN, which makes it easier to understand.

98
New cards

Modifiability

If you need to change the value, you only have to update it in one place (where it is first defined) instead of searching for every 100 in your code.

99
New cards

initialization

  • binding of a variable to a value at the time it is bound to storage

  • is often done on the declaration statement.