Principles of Programming Language - Chapter 5
Principles of Programming Language - Chapter 5
5.1 – Introduction
What’s This Chapter About?
This chapter introduces key ideas related to variables in programming
languages.
It covers:
How variables are linked to things like memory, types, and values
Chapter 5 1
Example: A 3D array needs extra logic (software) to manage how it’s
stored in memory.
Its name
Its value
Its lifetime
Its scope
You can give names to expressions, which look like assigning values to
variables.
C = C, C89, C99
Chapter 5 2
C-based languages refers to:
(Even though JavaScript and PHP look similar, they’re not treated as C-
based here.)
5.2 Names
What Are Names in Programming?
A name is a string of characters used to refer to something in a program.
Variables
Constants
Functions
Classes
Methods
Modules
Use variables
Organize functions
Call subroutines
Chapter 5 3
Lexemes and Names
✅ What Is a Lexeme?
A lexeme is the actual sequence of characters that make up a word or
symbol in your program.
Examples:
count
3.14
✅ What Is a Name?
A name is a lexeme that is associated with some entity in the language.
Example:
int x = 5;
In Fortran:
Chapter 5 4
Early versions limited names to 6 characters
This was because old machines could only read that much
In Python:
If it’s not case sensitive, then those three are considered the same.
✅ Examples
Language Case Sensitivity
Ada, Fortran No
Python Yes
These are usually allowed only under specific rules in each language.
Chapter 5 5
5.3 Variables
What Is a Variable in Programming?
A variable is a named container in memory that holds a value.
It acts like a label for a memory cell, so you can store and change
information in your program.
The name (like x ) helps us access or modify what's stored in those cells.
✅ 1. Name
The identifier used to refer to the variable.
✅ 2. Address
The memory location where the variable's value is stored.
Example:
int x = 10;
x is the name
Chapter 5 6
Its memory address could be something like 0x7ffe45a8
✅ 3. Value
The data stored in the memory cell.
You can read or change this value using the variable name.
Example:
x = 5;
✅ 4. Type
The kind of data the variable holds.
✅ 5. Lifetime
The time period during program execution when the variable exists in
memory.
✅ 6. Scope
The region of the program where the variable can be accessed.
Chapter 5 7
✅ 7. Binding
Binding is the connection between a variable and its properties (like its
type, memory address, or value).
Some bindings are done early (before the program runs), and some during
execution.
A name (like x )
1. A type
Example: x is an int
3. A value
Chapter 5 8
Example: x = 10
4. A function or method
5. A block of code
Definition
Binding time = The moment when a binding takes place.
3. Compile Time
Binds things like variable names to types (in statically typed languages).
4. Link Time
5. Load Time
6. Run Time
Chapter 5 9
Type When It Happens Example
Early Binding Before program runs C variable types fixed at compile time
Late Binding During execution Python can bind types at run time
Late binding gives more flexibility, but can be slower and more error-
prone.
✅ Static Binding
Happens before the program runs (at compile time or earlier)
Examples:
Variable types in C
✅ Dynamic Binding
Happens while the program is running
Examples:
Chapter 5 10
What kind of values the variable can hold
C++
Java
Ada
How It Works:
int x = 10;
Python
Ruby
JavaScript
Scheme
How It Works:
Chapter 5 11
x = 10 # x is an int
x = "hello" # now x is a string
This means:
Example in C#
The type is not written explicitly, but once bound, it cannot change.
Chapter 5 12
5.6 Storage Bindings and Memory
Management
What Is Storage Binding?
Storage binding is the process of assigning memory to a variable.
This is different from type binding — here, we’re talking about where in
memory the variable actually lives while the program is running.
It stays in that same memory location for the entire time the program runs.
Used For:
Global variables
Advantages:
Very efficient — memory is known early and access is fast.
Disadvantages:
Not flexible — once bound, the memory cannot be reused or resized.
Used For:
Chapter 5 13
Local variables in most functions
Example:
void myFunction() {
int x = 5; // memory for x is allocated when function is called
}
Advantages:
Supports recursion
Disadvantages:
Slower than static storage
Used For:
Objects and dynamic data structures like linked lists, trees, etc.
Examples:
In C:
In C++:
Chapter 5 14
int* p = new int;
In Java:
Advantages:
Very flexible — great for structures whose size is not known at compile
time.
Disadvantages:
Risk of memory leaks (if memory is not freed)
Slower performance
Used In:
Dynamically typed languages like Python, JavaScript, Lisp
Example (Python):
Advantages:
Very easy for programmers — no need to worry about memory
management.
Chapter 5 15
Disadvantages:
Slowest method
Languages: C, C++
What Is Lifetime?
✅ Definition
Lifetime is the time period during which a variable exists in memory —
from when it’s allocated to when it’s deallocated.
It's about how long the variable lives, not where it can be accessed.
Chapter 5 16
✅ Lifetime Depends on Storage Type
Storage Type When It’s Allocated When It’s Deallocated
What Is Scope?
✅ Definition
Scope is the part of the program where a variable can be used or
accessed by name.
A variable can only be used within its scope — otherwise it’s considered
undefined.
✅ Types of Scope
1. Static Scope (Lexical Scope)
The variable’s scope is determined by where it is written in the source
code.
You can tell what a name refers to just by looking at the code, without
running it.
Most modern languages (like C, Java, Python, etc.) use static scope.
Example:
int x = 5; // global x
void func() {
int x = 10; // local x
printf("%d", x); // prints 10
}
Chapter 5 17
The x inside func is in a different scope than the global x .
2. Dynamic Scope
The variable’s scope depends on the call stack at runtime, not on where it
is written in the code.
It checks for variable definitions in the calling functions, not in the text
above it.
Example:
main() {
int x = 5;
func();
}
func() {
print(x); // dynamically finds x in main’s environment
}
In dynamic scope, func would find x from main , because main called it.
✅ Local Scope
A variable defined inside a function or block is local to that block.
✅ Global Scope
A variable defined outside all functions is global.
Chapter 5 18
Nested Scopes and Visibility
✅ Nested Scopes
One scope inside another is a nested scope.
Inner scopes can see variables from outer scopes (unless overridden), but
outer scopes cannot see into inner ones.
int x = 1;
void outer() {
int y = 2;
void inner() {
int z = 3;
// x and y visible here
}
✅ Variable Shadowing
If a variable with the same name is declared in an inner scope, it hides the
one in the outer scope.
Block Structure
Most languages are block-structured, meaning each block (like a function
or loop) introduces a new scope.
Here is the paraphrased version of Section 5.8 – Scope Rules, rewritten clearly
for beginners and formatted cleanly for Google Docs.
Chapter 5 19
It preserves all original content — nothing skipped, only simplified for easier
understanding.
If a name is used in more than one place (like multiple variables named x ),
the scope rules decide which x the program refers to.
✅ How It Works
In static scope, the structure of the program’s code decides which variable
a name refers to — not the order of execution.
Most modern languages (like C, Java, Python, Ada) use static scope.
✅ Example:
Chapter 5 20
int x = 1;
void func() {
int y = 2;
void inner() {
int x = 3;
printf("%d", x); // prints 3, local x shadows global x
}
}
✅ Example (Pascal-style):
program Example;
var x: integer;
procedure A;
var y: integer;
procedure B;
var z: integer;
begin
x := 1; // accesses global x
y := 2; // accesses y from A
z := 3; // local z
end;
Chapter 5 21
begin
B;
end;
begin
A;
end.
In procedure B:
z is declared inside B
Nesting Levels
Referencing Environments
Chapter 5 22
5.9 Named Constants
A name
A type
A value
But unlike a variable, its value does not change after it is assigned.
✅ Example:
const int MAX_SIZE = 100;
✅ Benefits:
1. Improves Readability
Instead of seeing a random number like 100 , you see MAX_SIZE , which
tells you what the number means.
2. Easier Maintenance
If the value changes (e.g., 100 → 200), you only need to change it in
one place, instead of updating it everywhere in the code.
Since the value can't be changed accidentally, it makes the code safer
and more predictable.
Chapter 5 23
How Are Named Constants Defined in Different
Languages?
This means:
The type and value are fixed before the program runs.
Chapter 5 24
A name
A type
A value
But unlike a variable, its value does not change after it is assigned.
✅ Example:
const int MAX_SIZE = 100;
✅ Benefits:
1. Improves Readability
Instead of seeing a random number like 100 , you see MAX_SIZE , which
tells you what the number means.
2. Easier Maintenance
If the value changes (e.g., 100 → 200), you only need to change it in
one place, instead of updating it everywhere in the code.
Since the value can't be changed accidentally, it makes the code safer
and more predictable.
Chapter 5 25
Language How Named Constants Are Declared
This means:
The type and value are fixed before the program runs.
Chapter 5 26