5.1.7 · D3C Programming

Worked examples — Functions — declaration vs definition, prototypes, call by value

2,864 words13 min readBack to topic

The scenario matrix

Every call-by-value question is really one question: did the callee touch the caller's real memory, or only a photocopy? The answer depends on what you copied. Here is the full grid of cases this topic throws at you.

# Case class What is copied Does caller's data change?
A Plain int value, callee reassigns it the number No
B Two values swapped inside callee the two numbers No (classic swap trap)
C Pointer passed, callee dereferences the address Yes (edits through the copy)
D Pointer passed, callee reassigns the pointer itself the address No — the pointer copy moves
E Array passed to a function the address of element 0 Yes (arrays decay to pointers)
F Zero / degenerate input (0, NULL) a value / null address depends — must guard
G Return value ignored vs used nothing leaks; copy dies caller only sees what it stores
H Word problem (real world) domain values model it, then apply A–G
I Exam twist: prototype/definition mismatch compile error / silent bug

The six cases A, B, C, D are the mechanics; E, F, G are the edge conditions; H, I are application and traps. The worked examples below hit every cell.


Example 1 — Cell A: reassigning a plain value

Figure — Functions — declaration vs definition, prototypes, call by value

Example 2 — Cell B: the classic swap that doesn't swap

Figure — Functions — declaration vs definition, prototypes, call by value

Example 3 — Cell C: pointer passed, callee dereferences (the fix)

Figure — Functions — declaration vs definition, prototypes, call by value

See Pointers in C and Stack and Function Call Mechanism for the memory picture in depth.


Example 4 — Cell D: reassigning the pointer itself does nothing outside


Example 5 — Cell E: array passed to a function


Example 6 — Cell F: zero / degenerate inputs (guard the null)


Example 7 — Cell G: return value ignored vs stored


Example 8 — Cell H: real-world word problem


Example 9 — Cell I: the exam twist (prototype ≠ definition)


Recall Predict-the-output drill

int f(int x){x*=2; return x;} int a=4; int r=f(a); — values of r and a? ::: r = 8, a = 4 (copy changed, original safe). Passing &a and doing *p = 9 inside — does a change? ::: Yes, to 9 (dereferenced the copied address). Passing &a but doing p = &other then *p = 9 — does a change? ::: No — the pointer copy was redirected before the write. Array v passed to f(int arr[]) and arr[0]=7 set inside — does v[0] change? ::: Yes — arrays decay to a copied address, so writes reach the real array. addTax(p); on its own line with p=200 — new p? ::: Still 200; the returned copy was discarded.


Connections

  • Pointers in C — the tool that turns Cell A into Cell C.
  • Stack and Function Call Mechanism — where the copies live and die.
  • Arrays as Function Arguments — why arrays behave like Cell E.
  • Header Files and Separate Compilation — where prototype-vs-definition (Cell I) really bites.
  • Scope and Lifetime of Variables — why the callee's copies vanish on return.
  • Recursion — many frames, each with its own copies.