Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Online Courses / University Track / Computing Foundations

Module 6 · Lesson 2 · Core

Functions: inputs, output and tracing

Write a function as a named part with inputs and an output, and trace calls by hand.

  • 50 minutes
  • Serves O6
  • Assumes M6.L1

Phase 1: Prepare

Step 1 Warm-up recall

From M6.L1: name two parts of the receipt task.

Private: nothing you type here is sent or stored.

Step 2 Learning goal

By the end of this lesson you can:

  • define a function, input and output (Foundation);
  • trace a call by hand (Core);
  • write a short function from a description (Core).

Phase 2: Teach

Step 3 A familiar example

A calculator key such as the square root takes a number, gives back a number and hides the working. A function in a program does the same job for any step you name.

Step 4 Clear explanation

A function is a named part that takes inputs (also called arguments or parameters), does its work and gives back an output (the returned value). Using a function is a call. Here is an invented function: average(a, b) returns (a + b) ÷ 2.

Inputsa = 4, b = 10FunctionaverageOutput7
A function takes inputs, does its work out of sight and gives back an output.
CallWorkingResult
average(4, 10)(4 + 10) ÷ 27
average(3, 8)(3 + 8) ÷ 25.5
average(6, 6)(6 + 6) ÷ 26

To trace a call, replace the inputs by their values, do the working in order and write down the output. A function can be called many times with different inputs, so the working is written once and reused. A function should depend only on its inputs, so the same inputs always give the same output, which is what makes it easy to test. In a programming language, the rules for naming and calling functions differ in detail, but the idea is the same.

Step 5 Worked example (4 marks)

Trace average(6, 11) using average(a, b) = (a + b) ÷ 2.

  1. Replace the inputs: (6 + 11) ÷ 2. (1 mark)
  2. Add: 17 ÷ 2. (1 mark)
  3. Divide: 8.5. (1 mark)
  4. Answer: 8.5. (1 mark)

Phase 3: Practise

Step 6 Learner practice (instant-marked)

1. A function gives back:

2. average(8, 12) = (a + b) ÷ 2 equals:

3. The inputs to a function are also called:

4. Calling a function twice with the same inputs should give:

Phase 4: Close

Step 7 Recap

  • A function has inputs, working and an output.
  • Trace a call by replacing inputs with values.
  • The same inputs should give the same output.

Step 8 Exit ticket (ungraded)

Trace average(5, 9) and average(0, 7).

Private: nothing you type here is sent or stored.

Step 9 Go further (Extension)

Write a function double(x) that returns x × 2 and trace it for 6 and 0.

Common mistakes

MistakeWhy it fails
Mixing up the order of inputsMatch each input to its name in order.
Forgetting the outputA function must give something back.
Hiding extra inputs insideList every input the function needs.

Limits. The functions here are written in plain notation, not in a particular programming language.