Year 9 · Autumn 1

Python II: lists and strings · Lesson 5 of 5

Functions with parameters and return values

PracticalTopic 14 Programming further

Year 9 · Python II: lists and strings · Lesson 5 of 5

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.

Year 9 · Python II: lists and strings · Lesson 5 of 5

Success criteria

You have got it when you can say:

  • 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.

Year 9 · Python II: lists and strings · Lesson 5 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last lesson

    word = "Python". What is word[1:4]?

    A "yth"B "Pyth"C "ytho"

    A. "yth"

  2. Earlier this unit

    The program runs but gives the wrong answer. That's a…

    A Syntax errorB Runtime errorC Logic error

    C. Logic error

  3. Last year

    Program runs but gives the wrong answer. That's a…

    A Syntax errorB Logic error

    B. Logic error

Year 9 · Python II: lists and strings · Lesson 5 of 5

Key words (1 of 2)

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.

Year 9 · Python II: lists and strings · Lesson 5 of 5

Key words (2 of 2)

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 · step 1 of 1 · part 1 of 2

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.

The explanation · step 1 of 1 · part 2 of 2

Subroutines (continued)

  • 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.

Year 9 · Python II: lists and strings · Lesson 5 of 5

Try it: Test the score checker (0 to 50)

Year 9 · Python II: lists and strings · Lesson 5 of 5

Quick quiz · 1 of 3

In def area(w, h): … and a = area(4, 3), which are the arguments?

  1. A w and h
  2. B area and a
  3. C 4 and 3

C. 4 and 3

Arguments are the actual values passed in the call. w and h are parameters.

Year 9 · Python II: lists and strings · Lesson 5 of 5

Quick quiz · 2 of 3

A variable made inside a function is…

  1. A Local: only exists inside it while it runs
  2. B Global: usable anywhere
  3. C Saved to a file

A. Local: only exists inside it while it runs

Local variables are wiped when the function returns.

Year 9 · Python II: lists and strings · Lesson 5 of 5

Quick quiz · 3 of 3

Testing a 0–50 range with 50 is…

  1. A Erroneous data
  2. B Boundary data
  3. C Normal data

B. Boundary data

It's right at the edge of what's allowed.

Year 9 · Python II: lists and strings · Lesson 5 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. Typing your new password twice so they can be compared is…

    A ValidationB AuthenticationC Verification

    C. Verification Verification checks data was entered correctly. Double entry is one method.

  2. open("log.txt", "w") on a file that already holds 100 lines…

    A Gives an errorB Adds to the endC Wipes it first

    C. Wipes it first "w" empties the file before writing. Use "a" to append.

Year 9 · Python II: lists and strings · Lesson 5 of 5

Homework and the challenge

Homework

Study page 14 to the quiz.

End-of-unit challenge

A quiz program that stores questions in lists and uses at least two functions.