One button, many drinks
A coffee machine has a button called Latte. You press it. You do not need to know how it works inside.
Programmers make buttons like this. A button is a piece of code with a name. You write it once. Then you use it as often as you like.
Key words
| Word | What it means | Example |
|---|---|---|
| Function | A named piece of code that does a job and gives back a result. | len("cat") gives back 3 |
| Procedure | A named piece of code that does a job and gives back no result. | print("hi") shows text |
| Subroutine | The general word for a function or a procedure. | |
| Parameter | A name in the brackets ( ) of a definition. It receives a value. | def area(width, height) |
| Argument | The real value you send in when you call it. | area(3, 4) |
| Return value | The result a function gives back. | |
| Call | To use a function or a procedure by writing its name. | area(3, 4) |
| Variable | A name that holds a value. | |
| def | The Python word that starts a definition. |
The cells students fill in are left blank in this preview.
Why use them?
Without them you copy the same lines again and again. A mistake means fixing every copy.
With a subroutine you write the lines once and give them a name. You fix the mistake in one place. The program is also shorter and easier to read.
Try it: a procedure
def welcome():
print("Welcome to the bus!")
print("Please tap your card.")
welcome()
welcome()
# Call it a third time. Then change a message.
Sending values in
A subroutine may take parameters. They are names in the brackets. When you call it, you send arguments: the real values.
It may also take none. welcome() has empty brackets.
Try it: a parameter
def welcome(name):
print("Welcome,", name)
welcome("Sam")
welcome("Aisha")
# name is the parameter. "Sam" is the argument.
# Call it with your own name.
Giving a result back
A function must give back a result. It uses the word return. The code that called it can keep the result in a variable.
A procedure does a job, such as printing. It gives back no result.
Try it: a function
def ticket_cost(adults, children):
return adults * 2.5 + children * 1.0
total = ticket_cost(2, 3)
print("The total is", total)
print("Double it:", total * 2)
# Try 1 adult and 4 children.
Function or procedure?
| Function | Procedure | |
|---|---|---|
| Does a named job | Yes | Yes |
| May take parameters | Yes, or none | Yes, or none |
| Gives back a result | Yes, it must | No, it must not |
| Example | len(), max(), ticket_cost() | print(), welcome() |
The cells students fill in are left blank in this preview.
print is not return
data-callouts.warning.label
print shows text on the screen. return sends a value back to the code that called the function. A function that only prints gives back nothing. Python calls that value None.
Fix it: the missing return
def double(number):
print(number * 2)
answer = double(4)
print("The answer is", answer)
# It prints None. Change print to return. Then run it again.
Fix it: a missing argument
def greet(name, time_of_day):
print("Good", time_of_day + ",", name)
greet("Sam")
# Run it and read the message. The procedure needs two arguments.
# Add the second one, for example "morning".
Quick check: function or procedure?
Have a go
Quick check: function or procedure?
4 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.