Year 9 › Python II: lists and strings › Lesson 5 of 5
Year 9 · Autumn 1 · Python II: lists and strings Functions with parameters and return values
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 Thinking Cambridge 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.
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.
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") def defines it. Nothing runs yet.name is a parameter : a placeholder.greet("Ana") calls it. "Ana" goes into name.Procedure: does a job, returns nothing.Function : works something out and returns a value.★ RememberWhy 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 .
The steps for this lesson
Mark this lesson as done Saved on this device.
← Previous lesson Slicing and string methods End of unit → The end-of-unit challenge