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.
- Last lesson
word = "Python". What is word[1:4]?
A "yth"B "Pyth"C "ytho"
A. "yth"
- Earlier this unit
The program runs but gives the wrong answer. That's a…
A Syntax errorB Runtime errorC Logic error
C. Logic error
- 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")defdefines it. Nothing runs yet.nameis a parameter: a placeholder.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.
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?
- A w and h
- B area and a
- 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…
- A Local: only exists inside it while it runs
- B Global: usable anywhere
- 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…
- A Erroneous data
- B Boundary data
- 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.
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.
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.