Function Machine
Define once. Use forever.
Year 8 Computer Science — Functions & Parameters
The Mystery Machine
Give me a number. I give you a number back.
What's the rule?
Function Anatomy
DEFINE double_and_add_3 ( x ):
RETURN 2 × x + 3
- double_and_add_3 — the name (descriptive!)
- x — the parameter (input)
- 2 × x + 3 — the rule
- RETURN — sends the result back
Define vs Call
// DEFINE = write the recipe:
DEFINE double_and_add_3(x):
RETURN 2 × x + 3
// CALL = make the dish:
double_and_add_3(4) → returns 11
double_and_add_3(10) → returns 23
Define once. Call with different inputs every time.
Design Your Machine
- Group: secretly design a function — give it a name, a rule, test cases
- Write it on the worksheet (Part 2) — keep the rule hidden
- You will become "the machine" for other groups
Feed the Machine!
Rotate groups. Send input slips to each machine.
Machine group: compute and write the output.
After 6 inputs: guess the rule!
Guess the Rule!
After seeing 6 input/output pairs, each group guesses the function rule.
Authors: reveal your rule. Were the guesses correct?
Discussion: what made some functions easier/harder to guess?
Design More Functions
Complete worksheet Parts 3 and 4:
- Function 2: one parameter, your rule
- Function 3: two parameters
- Function chaining: `f(g(x))` — inner first, then outer
double_and_add_3(double_and_add_3(2))
→ inner: 2×2+3 = 7
→ outer: 2×7+3 = 17
Key Takeaway
A function is a named, reusable process.
Parameters make it flexible — different inputs, same logic.
Define once — call anywhere, with any arguments.
1 / 9
← → or click to advance