← 🧱 Runtime Environments
Subtopic 2 of 4

7.2Static vs dynamic scoping

Static (lexical): a name refers to the declaration in the enclosing text of where the function is written. Dynamic: it refers to the most recent binding on the call stack.

Global x10
g: local x20
f runs-
Output-
  • main calls g, g declares its own x = 20
  • g calls f. f prints x. Where was f WRITTEN? At global level.
  • x is resolved in f's defining environment: the global x
  • Output: 10

Program: int x = 10; void f() { print(x); } void g() { int x = 20; f(); } main() { g(); }

Previous year questions: Runtime Environments (2)

PYQs are grouped by topic, so this is the full set for Runtime Environments; try the ones that match this subtopic. Open all 2 on the topic page.