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

Free lesson

2.7 (1 of 3): Functions and procedures

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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.

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

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.

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

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.

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

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.

Make a free account to save your work and track your progress.

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

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

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

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".

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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

A flowchart: Start, then Input adults and children, then Call ticket_cost in the sub-process box, a rectangle with a second line down each side, then Output the total, then End.A flowchart: Start, then Input adults and children, then Call ticket_cost in the sub-process box, a rectangle with a second line down each side, then Output the total, then End.

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

Match 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.

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

Exit quiz

5 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

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.