Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Flashcards: open the module flashcards (end-of-module review with workplace questions).

Online Courses / University Track / Python from Zero

Module 5 · Lesson 1 · Core

Defining and calling a function

Write a function with def, call it by name and see why reuse beats copying.

  • 55 minutes
  • Serves O5
  • Assumes Module 4

Phase 1: Prepare

Step 1 Warm-up recall

From Module 4: how do you read the last item of a list?

Private: nothing you type here is sent or stored.

Step 2 Learning goal

By the end of this lesson you can:

  • define and call a function (Foundation);
  • explain why reuse helps (Core);
  • trace the order in which lines run (Core).

Phase 2: Teach

Step 3 A familiar example

A shop prints the same receipt footer on every sale. Typing it out each time invites mistakes, so it is written once and used again whenever it is needed.

Step 4 Clear explanation

A function is a named block of code. def defines it; writing its name with brackets calls it. The body is indented.

def greet():
    print("Welcome")
    print("Thank you")

greet()
greet()
Welcome
Thank you
Welcome
Thank you
Definedef name()Callname()Runthe bodyReturnto the caller
A function is a named recipe: define it once, then call it whenever you need it.

Defining a function does not run it. Python first reads the def, remembers the name, then runs the later lines. Each call jumps into the body and then returns to the line after the call. A function must be defined before the line that calls it runs.

The big gain is reuse. If the footer changes, you edit one function, and every call shows the new text. Copied code must be corrected in every copy, and one copy is usually missed.

Step 5 Worked example (4 marks)

Trace def hi(): print("A") followed by hi(), print("B"), hi().

  1. The def line only stores the function; nothing prints. (1 mark)
  2. The first call prints A. (1 mark)
  3. Then B is printed, then the second call prints A. (1 mark)
  4. Output: A, B, A. (1 mark)

Go deeper: the professor's notes

A function runs only when it is called. Defining it just stores the name.
def

Defines.

Call

name().

Body

Indented.

DefinedefStorethe nameCallname()Runthe body
Define then call

Two ways

StyleEffect
copy the codefix every copy
one functionfix once

At work: Footer text

A footer must change on every receipt.

  1. Put it in a function.
  2. Edit the one function.

What a careful practitioner decides. Use a function for any text or rule that repeats.

Phase 3: Practise

Step 6 Learner practice (instant-marked)

1. The keyword that defines a function is:

2. A function body runs when:

3. To call a function named show, write:

4. The main gain of a function is:

Phase 4: Close

Step 7 Recap

  • def defines; name() calls.
  • The body is indented and runs only on a call.
  • One function replaces many copies.

Step 8 Exit ticket (ungraded)

Write a function line() that prints ten dashes, then call it twice.

Private: nothing you type here is sent or stored.

Step 9 Go further (Extension)

Write a function that prints a three-line address block and call it for two customers.

In industry: Ariane 5: code reused without a check

The first Ariane 5 flight, on 4 June 1996, ended about 40 seconds after lift-off. Software reused from Ariane 4 converted a 64-bit floating-point number to a 16-bit signed integer; the value was larger than that integer could hold, and the conversion failed.

Link to this lesson. A function should check that its input fits what it was written for, especially when it is reused in a new setting.

Source: Report by the Inquiry Board, Ariane 5 Flight 501 Failure (19 July 1996)

Common mistakes

MistakeWhy it fails
Defining but never callingNothing runs until the call.
Forgetting the brackets in a callshow() calls; show only names it.
Calling before the def has runDefine first, call after.

Limits. This lesson uses functions with no inputs; inputs come next.

Flashcards for this lesson (3)

Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.

Card 1 of 3 · What is a function?
NameoneBodyindentedUsemany times
Function

A named block of code you can run whenever you need it. A tax calculation, written once and used on every sale, is a function.

Card 2 of 3 · How do you define and call one?
defdefinename()callBodyruns on call
def and call

Define with def name(): and an indented body; call with name(). The body runs only on a call.

Card 3 of 3 · Why use a function instead of copying code?
Copymany fixesFunctionone fixResultfewer errors
Reuse

One place to fix and to test. If the rule changes, every use is corrected. Copies drift apart and one is always missed.

Review with flashcards

Test yourself on this module with workplace flashcards and detailed answers.

Open the module flashcards