Quiz 2
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, b inside 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:
text
procedure Square(n):
    return n * n

x ← 3
y ← Square(x) + 1
y becomes 10.

Return vs print (side effect)

MechanismCaller getsUse
return vvalue vfurther 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:
cond1cond2OR
FFF
TFT
FTT
TTT
Contrast week 2 AND filter — both must hold.
Procedure sketch:
text
procedure 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.
text
procedure Max2(a, b):
    if a >= b:
        return a
    else:
        return b

m ← Max2(7, 5)   # m = 7
Example 2 — OR gate.
text
procedure Weekend(day):
    if day == "Sat" or day == "Sun":
        return True
    return False

Weekend("Sat") → True
Weekend("Mon") → False
Example 3 — Side effect only.
text
procedure Show(n):
    Print(n)
    # no return

x ← Show(4)   # displays 4; x not useful for math
Example 4 — Nested calls.
text
procedure 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)

  1. Trace:
text
procedure Times3(k):
    return k * 3

a ← 2
b ← Times3(a) + Times3(1)
What is b?
  1. Write OR condition in one line: flag True when code == 404 or code == 500.
  2. Procedure prints n*n but does not return. Caller runs y ← Square(5) then z ← y + 1. What can go wrong?
  3. procedure Pick(a, b): if a > 10 or b > 10: return a + b else: return 0. Evaluate Pick(3, 12) and Pick(8, 2).
  4. Difference between parameter and argument in one sentence each.
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.