SypherTec
…
PART 2 · 08 OF 084 MIN READUPDATED 20 NOV 2026

Return Values: Functions That Hand Back an Answer

Your print_line function prints, and that is all. Its result goes to the screen, where the rest of your program cannot use it. This lesson writes a function that hands its answer back, so you can store it and use it more than once.

The concept

Here is your tracker as the last lesson left it. Run it:

WHERE YOU LEFT OFF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Look at your expense loop. The counting is tangled into the printing, and it only ever counts against 3,000. Now run this:

A FUNCTION THAT HANDS BACK A COUNT · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

count_over contains the counting pattern from the accumulation lesson. Its last line is new: return found. return ends the function and hands the value of found back to the line that called it.

So count_over(expenses, 3000) becomes the number 2, and large = count_over(expenses, 3000) stores it. The second call counts against 1,000 instead, and gives back 4. One function, two questions, two answers your program can keep.

Now the wall. This trips up almost everyone. Run this:

PRINTING INSTEAD OF RETURNING · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Two lines appeared: 2, then Large spends: None. The function printed its count, so 2 reached the screen. But it returned nothing, so nothing came back to be stored. A function that ends without return gives back None, Python's value for nothing. That None is what large holds.

Printing shows a value to a person. Returning gives a value to your program. When the rest of your program needs the result, use return.

The syntax

def count_over(amounts, limit):
    found = 0
    for amount in amounts:
        if amount > limit:
            found = found + 1
    return found
  • return, then the value to hand back.
  • It sits inside the function's block. Here it is indented four spaces, level with the for, so it runs once, after the loop has finished.
  • return ends the function. Any lines after it in the block never run.

The call works like a value:

large = count_over(expenses, 3000)

Names created inside a function, like found, belong to that function. They exist while it runs, and are gone once it returns. The value comes out through return, and nowhere else.

A minimal working example

main.py
def count_over(amounts, limit):
    found = 0
    for amount in amounts:
        if amount > limit:
            found = found + 1
    return found
 
 
expenses = [1500, 2500, 3500, 800, 4200]
large = count_over(expenses, 3000)
print(f"Expenses over ₦3,000: {large}")

Output: Expenses over ₦3,000: 2.

Common mistakes

You print instead of returning. The demo above shows one symptom: None where a value should be. Here is the other one:

DOING ARITHMETIC WITH NOTHING · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

double(1500) printed 3000, then gave back None. Python cannot add 100 to nothing. NoneType is the type of None. When you see it in an error, look for a function that prints its result instead of returning it.

You use a name from inside the function. Run this:

A NAME FROM INSIDE THE FUNCTION · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
NameError: name 'found' is not defined. Did you mean: 'round'?

Ignore the suggestion: round is a built-in function with a similar spelling, and it has nothing to do with this. The function ran, and returned 2, but nothing stored it. found disappeared when the function ended. Store the returned value in a name of your own: large = count_over(...).

You write return outside a function. It gives SyntaxError: 'return' outside function. There is nothing to hand the value back to. Check the indentation: return must be inside a def block.

Try it yourself

You will replace the counter inside your loop with a function, and use it twice.

  1. Run the starting code below. Note the number on your Expenses over ₦3,000 line.

  2. Under your print_line function and its two blank lines, add count_over, followed by two blank lines:

    def count_over(amounts, limit):
        found = 0
        for amount in amounts:
            if amount > limit:
                found = found + 1
        return found
  3. Take the counting out of your expense loop. Delete the large_spends = 0 line above the for, and the two-line if inside it. The loop should only print each amount again.

  4. Under your left = budget - total line, add mid_spends = count_over(expenses, 1000) and large_spends = count_over(expenses, 3000). Above your Expenses over ₦3,000 line, add print(f"Expenses over ₦1,000: {mid_spends}"). Run it. Confirm the ₦3,000 line shows the same number as in step 1, and the new ₦1,000 line shows how many amounts are over 1,000. With the amounts given, that is 4.

  5. Break it on purpose: in count_over, change return found to print(found). Run it. Confirm two stray numbers appear at the very top of the output, and both Expenses over lines now say None.

  6. Fix it: change it back to return found. Run it. Confirm both lines show numbers again.

TRY IT YOURSELF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Your tracker now asks the same question with two different limits, using one function. That completes Part 2. The checkpoint next shows what your program can now decide, and what it still cannot describe.

QUIZ · 3 QUESTIONSCheck what stuckStart the quiz
PART 2 · 08 OF 08❮❯