On this page
State, Variables & Scope
Prev: Computational Thinking & Abstraction | Up: foundations | Next: Control Flow & Composition
Rel: Value vs. Reference Semantics, Call Stack & Heap
Ref: Wikipedia — Scope (Computer Science)
Tags: #public #prog #programming #foundations
Contents
Bindings vs. Storage Locations
Languages model variables in two primary ways:
- Memory box model (e.g., C, Rust): a variable names a typed memory slot; assignment copies bytes into that slot.
- Name-binding model (e.g., Python, JavaScript): a variable is a label pointing to an object on the heap; reassignment moves the label to a different object.
Mutability vs. Immutability
- Immutable bindings/values cannot change after creation, making code easier to reason about and safe to share across threads.
- Mutable state allows efficient in-place updates, but requires discipline to prevent unexpected aliasing side effects.
Scope vs. Lifetime
- Scope (compile-time / lexical): where in the source code a name is visible (block, function, module, global).
- Lifetime (runtime): how long the underlying value exists in memory. Closures and heap allocations can outlive the lexical scope that created them.
Linked from
Related notes
- tuts/programming/foundations Linked here; links to this page; shared tags: #foundations, #prog, #programming
- tuts/programming/foundations/computational_thinking_and_abstraction Linked here; links to this page; shared tags: #foundations, #prog, #programming
- tuts/programming/foundations/control_flow_and_composition Linked here; links to this page; shared tags: #foundations, #prog, #programming
- tuts/programming/data_and_types/value_vs_reference_semantics Linked here; shared tags: #prog, #programming
- tuts/programming/memory_and_execution/call_stack_and_heap Linked here; shared tags: #prog, #programming
- tuts/programming/foundations/functions_and_recursion shared tags: #foundations, #prog, #programming
- tuts/programming shared tags: #prog, #programming
- tuts/programming/algorithms_and_dsa shared tags: #prog, #programming
- tuts/programming/algorithms_and_dsa/complexity_and_big_o shared tags: #prog, #programming
- tuts/programming/algorithms_and_dsa/linear_and_hashed_structures shared tags: #prog, #programming