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.

2
?
7
5
?
13
0
?
3

What's the rule?

Function Anatomy

DEFINE double_and_add_3 ( x ):
    RETURN 2 × x + 3

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

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:

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