Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Functions and procedures

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

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

WordWhat it meansExample
FunctionA named piece of code that does a job and gives back a result.len("cat") gives back 3
ProcedureA named piece of code that does a job and gives back no result.print("hi") shows text
SubroutineThe general word for a function or a procedure.
ParameterA name in the brackets ( ) of a definition. It receives a value.def area(width, height)
ArgumentThe real value you send in when you call it.area(3, 4)
Return valueThe result a function gives back.
CallTo use a function or a procedure by writing its name.area(3, 4)
VariableA name that holds a value.
defThe 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

Code: python
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

Code: python
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

Code: python
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?

FunctionProcedure
Does a named jobYesYes
May take parametersYes, or noneYes, or none
Gives back a resultYes, it mustNo, it must not
Examplelen(), 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

Code: python
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

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The lesson goes on

4 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.