Mansori

Functions Are Input-Output Machines

About this lesson, for parents and teachers

Every app you use is built from small machines called functions. Feed one a number, it hands you back an answer. Can you crack what these mystery machines do inside?

Ages 12–14 · about 7 minutes · builds on Variables Are Labeled Boxes · leads to Recursion Grows a Tree

Try this

  1. Tap a number tile to feed it into machine f. Watch what comes out.
  2. Run a few tests, then pick the code you think is inside. A wrong guess is checked against your own tests.
  3. Crack machine g the same way. Careful: two rules can agree on one test and disagree on the next.
  4. Predict g(f(3)), then watch the number flow through both machines.

How it works

A function is a named piece of code that takes an input (also called an argument), does some work, and gives back an output (the return value). In JavaScript, function f(x) { return x * 2 + 1; } is a machine named f: put 3 in and 7 comes out. The same input always gives the same output.

When you do not know what is inside a function, you can still learn a lot by testing it. That is what programmers do with a library they did not write: try inputs, look at outputs, form a hypothesis, then test more. One test is rarely enough. For example x * 2 - 1 and x + 1 both turn 2 into 3, but they disagree on every other number.

Functions can be chained, which means the output of one becomes the input of the next. g(f(3)) runs f first, getting 7, then runs g on 7. Order matters: f(g(3)) runs g first, turning 3 into 8, then f turns 8 into 17, a different answer. Mathematicians call this composition. Programmers chain functions all the time, for example cleaning up some text, then splitting it into words, then counting them.

Why do engineers love functions? You write the code once and reuse it everywhere. You can give it a clear name, test it on its own, and fix a bug in one place. Large programs are mostly small functions calling other functions, the same way a factory is a line of machines feeding each other.

Where engineers use this

Spreadsheet formulas like SUM, a game's damage calculation and the code that resizes your photos are all functions. Engineers write automated tests that feed functions inputs and check the outputs, just like you did here.

Words to know

Function
A named, reusable block of code that takes inputs and returns an output.
Argument
The value you pass into a function, like the 3 in f(3).
Return value
The answer a function hands back when it finishes.
Composition
Chaining functions so one function's output is the next one's input, written g(f(x)).

Talk about it

Goal: Feed numbers into mystery machines, guess the rule, and chain functions together.

  • Why was one test not enough to be sure about the rule?
  • What is a function you use every day without seeing its code, like a calculator button?
  • Does g(f(x)) always equal f(g(x))? Can you find a case where it does?

Without a screen: Play 'What's my rule?': one person secretly picks a rule (like double it then subtract 1), the other says numbers and hears the outputs, writing them in an in/out table on paper. Swap roles, then try chaining two people's rules.

Curriculum links: CSTA K-12 2-AP-14 (procedures with parameters), 2-AP-13 (decomposition); Common Core 8.F.A.1 (a function assigns exactly one output to each input)

Questions

What is a function in coding?
A function is a named block of code that takes an input, does something with it, and returns an output. You write it once and can call it as many times as you like with different inputs.

Is a function in coding the same as a function in math?
They are closely related. A math function maps each input to exactly one output, like f(x) = 2x + 1. A coding function can do that, and can also do other things like draw on the screen or save a file.

What does g(f(x)) mean?
It means run f on x first, then feed f's answer into g. It is called function composition, and the order matters: f(g(x)) usually gives a different result.