Neural Sync Active
computationalthinking-week3
Registry Synced
computationalthinking-week3
672 words
3 min read
Reading compass
Now · Week map
Procedures package reusable steps. Week 3 adds parameters, return vs side effect, and OR selection patterns.
Week map
Decompose problem → procedure definition → parameters vs arguments → return to caller → side effects → OR predicates → call-stack trace.
Procedure notation
- Procedure / function → named block with parameters →
procedure Add(a, b). - Parameter → placeholder in definition →
a,binside body. - Argument → actual value at call →
Add(3, 5)passes 3 and 5. - Return → value replaces call expression →
x ← Add(2, 4)→x = 6. - Side effect → changes external state or prints → return may be absent.
Call flow: caller pauses → callee runs → returns to line after call.
Mini-trace:
textprocedure Square(n): return n * n x ← 3 y ← Square(x) + 1
y becomes 10.Return vs print (side effect)
| Mechanism | Caller gets | Use |
|---|---|---|
return v | value v | further computation |
Print(v) | nothing (display) | human output |
Quiz often asks final variable — trace
return, not display.OR selection
OR gate: pass if any condition true.
Eligibility: “scholarship if grade A or score > 90” → one true suffices.
Truth mini-table for OR:
| cond1 | cond2 | OR |
|---|---|---|
| F | F | F |
| T | F | T |
| F | T | T |
| T | T | T |
Contrast week 2 AND filter — both must hold.
Procedure sketch:
textprocedure Eligible(grade, score): if grade == "A" or score > 90: return True else: return False
Pattern families
Easy — Trace one call
- Substitute arguments into parameters.
- Single return to assignment.
- Parameter shadows outer name — inner definition wins inside body.
Medium — Return vs side effect
- Procedure prints but caller assigns from missing return →
None-like bug. - Mutating input list as side effect — caller’s list changes.
- Two calls in one expression:
Add(1,2) + Add(3,4).
Hard — OR procedure design
- Truth table from word problem → procedure returning category.
- Nested call:
Double(Square(3)). - Parameter count mismatch at call — error or wrong binding.
Worked mini-examples
Example 1 — Simple return.
textprocedure Max2(a, b): if a >= b: return a else: return b m ← Max2(7, 5) # m = 7
Example 2 — OR gate.
textprocedure Weekend(day): if day == "Sat" or day == "Sun": return True return False Weekend("Sat") → True Weekend("Mon") → False
Example 3 — Side effect only.
textprocedure Show(n): Print(n) # no return x ← Show(4) # displays 4; x not useful for math
Example 4 — Nested calls.
textprocedure Inc(n): return n + 1 Inc(Inc(5)) → Inc(6) → 7
Example 5 — AND vs OR trap.
“Free if member or senior” → OR. “Free if member and coupon” → AND. Procedure must match wording.
Traps
- Ignoring return value when assignment expected.
- Parameter name equals global variable — trace inside procedure carefully.
- Side effect mutating caller’s list without expecting it.
- OR when problem requires all conditions (AND).
- Wrong argument order if parameters positional.
Diagnostic (try yourself)
- Trace:
textprocedure Times3(k): return k * 3 a ← 2 b ← Times3(a) + Times3(1)
What is
b?-
Write OR condition in one line:
flagTrue whencode == 404orcode == 500. -
Procedure prints
n*nbut does not return. Caller runsy ← Square(5)thenz ← y + 1. What can go wrong? -
procedure Pick(a, b): if a > 10 or b > 10: return a + b else: return 0. EvaluatePick(3, 12)andPick(8, 2). -
Difference between parameter and argument in one sentence each.