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.
Write it once, use it often
Without subroutines 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.
Make a free account to save your work and track your progress.
Parameters take 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.
Make a free account to save your work and track your progress.
A function gives 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.
Make a free account to save your work and track your progress.
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
Warning
Many people think print gives a value back. It only shows text on the screen. return sends a value back to the code that called the function. A subroutine that only prints gives back None.
Fix it: the missing return
def double(number):
print(number * 2)
answer = double(4)
print("The answer is", answer)
# The answer is None. Change print to return so the function gives the number back. Then run it again.
Make a free account to save your work and track your progress.
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".
Make a free account to save your work and track your progress.
Watch: what goes in and what comes back
Press Play on the short film below. It defines a procedure and a function, calls them, and follows each value: the argument going in, and the result coming back to the call. Watch what comes back when the body only prints.
Film: functions and procedures
Functions and procedures
Every scene, as text
- A subroutine is a named piece of code, and def starts its definition. In def welcome(name):, name is a parameter: a name in the brackets that receives a value. The indented line is the body. Defining it does not run the body yet.
- welcome("Sam") calls it. "Sam" is the argument, the real value sent in, so name is set to "Sam" and the body prints Welcome, Sam. welcome("Aisha") runs the body again and prints Welcome, Aisha. A procedure does a job and gives back no result.
- Myth (false): print gives a value back. answer = double(4) sends in 4, so number is set to 4 and the body prints 8. But print only shows it on the screen. Nothing is returned, so the call gives back None: answer is set to None, and the last line prints The answer is None.
- The cure: change print to return. return number * 2 works out 8 and sends it back to the code that called the function. answer is set to 8, and the last line prints The answer is 8.
- A function gives back a result with return, and the code that called it can keep it in a variable: answer is set to 8. A procedure does a job, such as printing, and gives back no result: result = welcome("Sam") prints Welcome, Sam, and result is set to None. Python starts both with def.
A call as a flowchart
Quick check: function or procedure?
Have a go
Quick check: function or procedure?
4 questionsMatch the part to its job
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Your turn: four subroutines
Finish four subroutines. area(width, height) is a function: give back width times height. welcome(name) is a procedure: print exactly Welcome, Sam! when the name is Sam. There is a comma and a space after Welcome, and no space before the exclamation mark. Return nothing. bigger(a, b) is a function: give back the larger number. lucky_number() is a function with no parameters: give back 7. Press Run Tests to check.
def area(width, height):
# A function. Give back the area of a rectangle.
pass
def welcome(name):
# A procedure. Print: Welcome, Sam! (with the name that was sent in)
# Do not return anything.
pass
def bigger(a, b):
# A function. Give back the larger of the two numbers.
# Do not use the built-in max() function.
pass
def lucky_number():
# A function with no parameters. Give back the number 7.
pass
Make a free account to save your work, or press Start lab to work through the whole lesson.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Exit quiz
Have a go