Quarry School

Give a repeated task a name

Subject: Computing
Level 1 · FoundationsAbout 5 min read3 new terms
How this was estimated

Programmatic estimate. Rung levels, worked steps, prerequisites, terms and sentence length contribute to this level. It describes the lesson, not your ability.

Reading counts the whole lesson, including folded explanations, at 180 words a minute. Practice allows two minutes per item. These are planning assumptions; take the time you need.

AI estimate unavailable.

Path to here

Open the unit’s lesson list to follow the lesson order.

    Leans on

    Quick review (2 minutes)

    In plain words

    A recipe card titled Tea sits in a kitchen drawer.

    Rule

    A def line and its indented body define a function without running the body. The functions of this lesson have no parameters. Once such a function has been defined, a call, written as its name followed by an empty pair of parentheses, runs the body from its first line. When the body finishes, the program carries on from the place of the call. Each such call runs the body once.

    Step

    1. Find the lines that the program needs in more than one place.

    2. Write def, a name that says what the lines do, an empty pair of parentheses and a colon.

    3. Put the lines under the def line and indent them one more level, four spaces, keeping any deeper indentation they already have.

    4. Leave the indented block, and write the function's name with parentheses at each place where the lines should run.

    5. Trace the program: skip the body at the def line, and run the body at each call.

    Trap

    The parentheses make the call. On a line below the function's definition, the name alone, show_banner with no parentheses, does not call it. The file displays no banner and reports no error. Check for the parentheses when a function seems to do nothing.

    The body belongs to the function only while it is indented. A print line at the margin, after the body, is not part of the function. It runs once, when Python reaches it, and not at each call.

    1 In plain words

    A recipe card titled Tea sits in a kitchen drawer. Writing the card makes no tea. When someone says to make tea, the cook takes out the card and follows its steps, and the card is there again the next time.

    A function is a block of lines with a name. A def line starts it: the word def, the function's name, a pair of parentheses and a colon. The indented lines under it are the function's body.

    Defining a function does not run its body. The body runs when the defined function is called, and it runs again at every such call. In this lesson a call to one of your own functions is its name followed by an empty pair of parentheses, such as show_banner().

    Remember one sentence: def writes the card, and a call follows it. To look it up, see section 4.8 of the Python 3.13 tutorial, on defining functions.

    2 Picture it

    LineWhat it doesdef linenames the bodybodywaits to be runcallruns the body

    A table of three parts of a program and what each does. The def line names the body. The body waits to be run. A call runs the body. The row for the call is marked.

    What each part of the banner program does

    3 Why

    Python reads a file from the top. When it reaches a def line it stores the body under the function's name and moves on to the first line after the body. Nothing is displayed, because the body has not run.

    At a call of a function that has been defined, Python goes to the stored body, runs its lines in order and then carries on from the place of the call. The banner program calls show_banner twice, so the three lines of its body run twice.

    A name has to have a value before it is used. In a file where nothing else has defined show_banner, a call that runs before the def line stops the program with a NameError.

    4 The rule

    A def line and its indented body define a function without running the body.

    The functions of this lesson have no parameters. Once such a function has been defined, a call, written as its name followed by an empty pair of parentheses, runs the body from its first line. When the body finishes, the program carries on from the place of the call. Each such call runs the body once.

    5 How

    1. Find the lines that the program needs in more than one place.
    2. Write def, a name that says what the lines do, an empty pair of parentheses and a colon.
    3. Put the lines under the def line and indent them one more level, four spaces, keeping any deeper indentation they already have.
    4. Leave the indented block, and write the function's name with parentheses at each place where the lines should run.
    5. Trace the program: skip the body at the def line, and run the body at each call.

    6 Worked example

    Call one function twice

    Python code
    def show_banner():
        print("============")
        print("LOST AND FOUND")
        print("============")
    
    show_banner()
    print("Blue scarf")
    show_banner()
    

    Standard input

    Empty

    Standard output

    ============
    LOST AND FOUND
    ============
    Blue scarf
    ============
    LOST AND FOUND
    ============
    

    Predict the seven output lines first. Then save the code as banner.py and run the file, or follow the trace below. The body runs once for each call.

    Call the function too early

    Python code
    show_banner()
    
    def show_banner():
        print("LOST AND FOUND")
    

    Standard input

    Empty

    Standard output

    Empty

    Exception NameError

    Save the four lines as early.py and run the file. The call is on line 1, above the def line, so Python does not know the name yet and stops with a NameError. Move the call below the function to repair the program.

    Trace

    Trace the banner program
    InstructionWhat Python doesOutputExplanation
    def show_banner():Stores the three body lines under the name—Nothing is displayed. Python moves to the first line after the body.
    show_banner()Runs the bodythe three banner linesThe first call. The body's three print lines run in order.
    print("Blue scarf")RunsBlue scarfPython has come back from the call and carries on.
    show_banner()Runs the body againthe three banner linesThe second call runs the same three lines.

    7 The trap

    The parentheses make the call. On a line below the function's definition, the name alone, show_banner with no parentheses, does not call it. The file displays no banner and reports no error. Check for the parentheses when a function seems to do nothing.

    The body belongs to the function only while it is indented. A print line at the margin, after the body, is not part of the function. It runs once, when Python reaches it, and not at each call.

    Quick checks

    Quick check 1 of 2

    Predict what this program displays. The program reads no input.

    Python code
    def chime():
        print("ding")
    
    print("start")
    chime()
    chime()
    print("end")
    Show the steps and the answer
    Answer

    It displays start, ding, ding and end, each on its own line.

    Why: The def line displays nothing. The first print runs, then each call runs the body once, then the last print runs.

    Source: Python Software Foundation, Python 3.13 Language Reference, 8.7 Function definitions

    Quick check 2 of 2

    A file holds only these two lines. What does it display when it is run?

    Python code
    def hello():
        print("hello")
    Show the steps and the answer
    Answer

    It displays nothing.

    Why: The file defines the function and never calls it. The body runs only at a call.

    Source: Python Software Foundation, Python 3.13 Language Reference, 8.7 Function definitions

    Sources

    1. Python 3.13 Tutorial, 4.8 Defining Functions. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    2. Python 3.13 Language Reference, 8.7 Function definitions. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    3. Python 3.13 Language Reference, 6.3.4 Calls. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    4. Python 3.13 Library Reference, Built-in Exceptions: NameError. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    5. Python 3.13 Tutorial, 4.10 Intermezzo: Coding Style. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    6. Python 3.13 Language Reference, 7.1 Expression statements. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.