Quarry School

Return a result

Subject: Computing
Level 1 · FoundationsAbout 6 min read1 new term
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.

    Quick review (2 minutes)

    In plain words

    A shop has a price board on the wall and a clerk with a calculator.

    Rule

    A return line works out its expression, ends the call and gives that value back to the calling line; a return with no expression gives back None. A call whose body runs to its end without reaching a return line gives back None, and that includes a body that prints its result.

    Step

    1. Decide what value the rest of the program needs from the function.

    2. Write return followed by that value, or by the expression that works it out, at each point where the function's work is done. In the first worked example that is the last line of the body.

    3. At the call, do something with the value: give it a name, print it or use it in an expression.

    4. Trace a call by replacing it with the value it returns, then finishing the line the call was in.

    5. If a function should only display something, leave return out and call it on a line of its own.

    Trap

    Printing inside a function is not returning. A function whose body prints its result and has no return line gives back None. A line such as total = show_double(5) + 1 then displays 10 and stops with a TypeError when Python tries to add None and 1. A TypeError is the error for an operation applied to a value of the wrong type. Use return when the calling line needs the value.

    The opposite slip is to return a value and drop it. A line that holds only total_price(4, 3) runs the function, and in a file that is run it displays nothing. Python writes out the value of such a line only when the line is typed in the Shell and the value is not None.

    1 In plain words

    A shop has a price board on the wall and a clerk with a calculator. The board shows a number to anyone who looks. The clerk works out a number and hands it to you on a slip, and you can add it to your own sums.

    print is the board: it displays a value for a person to read. A return line is the clerk. It ends the call and hands a value back to the line that made the call. That value is the function's return value.

    A call that returns a value can stand wherever a value can. The program can give it a name, print it or use it in a calculation.

    A function that reaches the end of its body without a return line still gives something back: the value None, which stands for nothing.

    Remember one sentence: print shows a value, and return hands it back. To look it up, see section 7.6 of the Python 3.13 language reference, on the return statement.

    2 Picture it

    CallDisplaysGives backshow_double(5)10Nonegive_double(5)nothing10

    A table of two calls. The call show_double(5) displays 10 and gives back None. The call give_double(5) displays nothing and gives back 10. The row for give_double is marked.

    A function that prints and a function that returns

    3 Why

    When Python reaches a return line it works out the expression after the word return, leaves the function at once and puts that value where the call was. A return with nothing after it gives back None. In bill = total_price(4, 3) the call becomes 12, and the name bill is tied to 12.

    Because return leaves the function, no later line of the body runs in that call. A function can hold more than one return line, and the first one Python reaches ends the call.

    A call that finishes normally gives some value back. When the body has no return line, that value is None.

    4 The rule

    A return line works out its expression, ends the call and gives that value back to the calling line; a return with no expression gives back None.

    A call whose body runs to its end without reaching a return line gives back None, and that includes a body that prints its result.

    5 How

    1. Decide what value the rest of the program needs from the function.
    2. Write return followed by that value, or by the expression that works it out, at each point where the function's work is done. In the first worked example that is the last line of the body.
    3. At the call, do something with the value: give it a name, print it or use it in an expression.
    4. Trace a call by replacing it with the value it returns, then finishing the line the call was in.
    5. If a function should only display something, leave return out and call it on a line of its own.

    6 Worked example

    Use a returned price twice

    Python code
    def total_price(price, count):
        return price * count
    
    bill = total_price(4, 3)
    print(bill)
    print(total_price(2, 5) + 1)
    

    Standard input

    Empty

    Standard output

    12
    11
    

    Predict the two output lines first. Then save the code as price.py and run the file, or follow the trace below. Each call is replaced by the value it returns.

    Print in one function and return from another

    Python code
    def show_double(n):
        print(n * 2)
    
    def give_double(n):
        return n * 2
    
    result = show_double(5)
    print(result)
    print(give_double(5))
    

    Standard input

    Empty

    Standard output

    10
    None
    10
    

    Predict the three output lines first. Then save the code as double.py and run the file. The body of show_double displays 10. It has no return line, so the call gives back None, and that is what the second output line shows. give_double displays nothing itself. It returns 10, and the last line prints that value.

    Leave a function at the first return

    Python code
    def late_fee(days):
        if days == 0:
            return 0
        return 2
    
    print(late_fee(0))
    print(late_fee(4))
    

    Standard input

    Empty

    Standard output

    0
    2
    

    Predict the two output lines first. Then save the code as fee.py and run the file. For 0 days the condition is True and the first return line ends the call, so the second is never reached. For 4 days the condition is False, the indented block is skipped and the call ends at the second return line.

    Trace

    Trace the price program
    InstructionCall becomesOutputExplanation
    def total_price(price, count):——The def line stores the body.
    bill = total_price(4, 3)12—The call ties price to 4 and count to 3. The return line gives back 4 * 3. bill now names 12.
    print(bill)—12The value the function returned is displayed.
    print(total_price(2, 5) + 1)1011The call is worked out first and gives back 10. Then 10 + 1 is worked out and displayed.

    7 The trap

    Printing inside a function is not returning. A function whose body prints its result and has no return line gives back None. A line such as total = show_double(5) + 1 then displays 10 and stops with a TypeError when Python tries to add None and 1. A TypeError is the error for an operation applied to a value of the wrong type. Use return when the calling line needs the value.

    The opposite slip is to return a value and drop it. A line that holds only total_price(4, 3) runs the function, and in a file that is run it displays nothing. Python writes out the value of such a line only when the line is typed in the Shell and the value is not None.

    Quick checks

    Quick check 1 of 2

    Predict the two lines this program displays. The program reads no input.

    Python code
    def area(width, height):
        return width * height
    
    print(area(3, 4))
    print(area(2, 2) + area(1, 5))
    Show the steps and the answer
    Answer

    It displays 12 and then 9.

    Why: The first call returns 3 * 4. In the second line the calls return 4 and 5, and 4 + 5 is 9.

    Source: Python Software Foundation, Python 3.13 Language Reference, 7.6 The return statement

    Quick check 2 of 2

    What does this program display, and why is the second line not a number?

    Python code
    def show_sum(a, b):
        print(a + b)
    
    answer = show_sum(2, 3)
    print(answer)
    Show the steps and the answer
    Answer

    It displays 5 and then None.

    Why: The body prints 5. It has no return line, so the call gives back None, and answer names None.

    Source: Python Software Foundation, Python 3.13 Tutorial, 4.8 Defining Functions

    Sources

    1. Python 3.13 Language Reference, 7.6 The return statement. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    2. Python 3.13 Language Reference, 6.3.4 Calls. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    3. Python 3.13 Tutorial, 4.8 Defining Functions. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    4. Python 3.13 Language Reference, 8.1 The if statement. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    5. Python 3.13 Library Reference, Built-in Exceptions: TypeError. 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.