Skip to the page
Year 9 · Autumn 1 · Python II: lists and strings

Functions with parameters and return values

PracticalPresent ▶

What this lesson covers

Topic 14 Programming further

KS3 Computing · Real programs keep data in lists, take text apart as strings, split work into subroutines, check their input, save to files, and get tested on purpose.

Programming Computational ThinkingCambridge Lower Secondary Computing 0860

You will learn to

  • Write a function with def that takes parameters and returns a value.
  • Explain the difference between print, which shows a value, and return, which hands it back.
  • Call one function three times with different values.
  • Write a function that checks a score from 0 to 50 and returns True or False.

Success criteria

  • I can write area(w, h) and use its answers in a sum, like area(3, 4) + area(2, 5).
  • I can explain why a function that only prints cannot be used in a sum.
  • I can test my score checker with 25, 0, 50, -1 and 51, and say what each should give.

Key words

Say each word, then read what it means. Every one is in the glossary too.

subroutine
A named block of code you can call when needed.
parameter
A placeholder in a subroutine for a value passed in.
Function
A subroutine that returns a value.
List
One name holding many values in order. Also called an array.
Index
An item's position in a list, counting from 0.
Validation
Checking input is reasonable before using it.
Boundary data
Test data at the very edge of what's allowed.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

Subroutines

A subroutine is a named chunk of code you can run whenever you need it. Scratch calls these 'My Blocks'.

def greet(name):
    print("Hello, " + name + "!")

greet("Ana")
greet("Leo")
  1. def defines it. Nothing runs yet.
  2. name is a parameter: a placeholder.
  3. greet("Ana") calls it. "Ana" goes into name.
  • Procedure: does a job, returns nothing.
  • Function: works something out and returns a value.
★Remember

Why bother? No repeated code. Fix a bug once. Each part is small and testable. Teams can split the work.

Scope: a variable made inside a subroutine is local: it only exists in there. One made in the main program is global.

Study it

Open the study page and go one step at a time.

The steps for this lesson

Try it

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.