Semantic Analysis · Compiler Design

Parameter Passing

Watch lectures

Parameter Passing

1. Why It Matters

Parameter-passing mode decides what the callee receives (value? address? text?) and whether assignments inside affect the caller. Choice affects correctness + code gen.

2. Four Modes (exam table)

Mode Callee gets Change visible? Example
Call by value copy of value No C default: f(x)
Call by reference address (alias) Yes C++ f(int &x), Pascal var
Call by copy-restore (value-result) copy in, copy out on return Yes (except aliasing differs) Ada in out
Call by name unevaluated thunk re-evaluated per use Yes, subtle (Jensen's device) Algol 60

3. Worked Difference

x=10; f(x){ x=x+1; } print(x)
// value → prints 10; reference → prints 11
// copy-restore → prints 11 (copied back at return)
// name with f(a[i]) → re-evaluates a[i] each mention (i changes → different cell!)

Aliasing trap: f(a,a) by reference makes two params one cell; copy-restore copies back in order → different result.

4. Implementation

  • Value: push rvalue; Reference: push lvalue address; Copy-restore: push address of temp + copy; Name: push thunk closure.

5. Exam Points

  • Compare 4 modes with example — 7 marks, very frequent.
  • f(a,a) aliasing difference — 5 marks, frequent.
  • Which languages use which — 3 marks, frequent.
Student FinanceWeb Platform

Pocketly — Personal Finance for Students

Keep your hostel and college expenses under control. Track accounts, log spending in seconds, and stay within your monthly budget.

← All Compiler Design modules