Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Programming Fundamentals · Grade 6-8

Return Values

The value that a function sends back to the code that called it, specified by the return statement.


Quick Answer

The value that a function sends back to the code that called it, specified by the return statement. Use it whenever the caller needs the function's result to keep working: total = add_tax(20) stores the answer, and add_tax(20) + 5 uses it straight away. As soon as return runs, the function stops, and the call is replaced by the value it sent back. Printing is different: print shows a value on the screen but hands nothing back for the program to use.

Why This Matters

Return values let functions snap together like building blocks: area = width(plan) * height(plan), or price = round(add_tax(cost), 2), where each piece hands its result to the next. A function that only prints its answer looks right on the screen, but in Python the caller receives None, so the next calculation crashes. That print-instead-of-return slip is one of the most common beginner bugs.

Intuitive Explanation

Ask a friend to check the price of a drink at the shop. They go, look, come back and tell you the number, and then you decide what to do with it. The friend is the function, your request is the call, and the number they bring back is the return value. In code: def double(x): return x * 2. The line result = double(5) runs the function with x = 5, the function sends back 10, and the call double(5) is replaced by 10, so result holds 10.

Because a call turns into its value, a call can sit anywhere a value can: double(5) + 1 is 11, and double(double(3)) is double(6), which is 12. return also ends the function at once. In a function sign(n) whose body is if n < 0: return 'negative' followed by return 'not negative', a negative input never reaches the second line, because the first return has already left the function.

The look-alike is print. A function show_double(x) whose body is print(x * 2) puts 10 on the screen, which seems like the same result. But the caller receives nothing: in Python, result = show_double(5) stores None, and result + 1 then crashes. Ask who needs the value. If a person needs to see it, print it; if the program needs to keep calculating with it, return it.

Think of it like this. A return value is the receipt handed to you at a shop counter: the work happens behind the counter, but the receipt is the one thing you carry away and can use later, for example to add up your spending. Once it is handed over, that visit is finished. Unlike a shop, a function hands back exactly one value per call, though that value can be a list.

Core idea. Return values let functions communicate results back to their callers. A function without a return value just performs an action.


When to Use

Use a return value when the code that calls a function needs the result to store, compare or pass on: a computed total, a yes/no check such as is_valid(email), a converted measurement. Signals are phrases like calculate and use or send back, and a call written on the right of = or inside another expression. If a function's job is only to do something visible, like drawing a shape, it may not need to return anything. Sharing values through global variables instead of returning them is usually a scope problem waiting to happen.

Tip. Ask: Does the calling code need the function's result to store, compare or pass on, rather than just see it printed?

How to Recognize It

Follow the value a function produces and see where it goes once the call ends: into a variable, into another calculation, onto the screen, or nowhere.

  1. Is the call used as part of a larger line, as in total = price(3) + 2 or if is_valid(pin)?

    Yes means the function must return a value, because the call is replaced by whatever comes back. A function that only prints would hand back None there.

  2. Does the function only need to show something to the user and then stop?

    Yes means printing, which is Input/Output, may be enough, and no return is needed. If any later line uses the result, the function needs a return as well.

  3. Does the function body contain more than one return, such as one inside an if?

    Yes means the first return that runs ends the call, so trace which branch runs and skip the lines after it. If no return is reached, Python sends back None.

  4. Is the value going into the function rather than coming out of it?

    Yes means you are looking at Parameters, the inputs named in the definition. A return value travels the other way, from the function back to the caller.

Return Values vs Input/Output vs Parameters

Printing, returning and passing arguments all move a value between a function and the rest of the program, and on screen they can look alike. Ask which way the value travels and whether the program can still use it afterwards.

Return Values

Meaning
A return value is the single result a function hands back to the line that called it, ending the call.
Key test
The program can store or reuse it; the call is replaced by the value.
Example
def half(n): return n / 2 makes x = half(10) + 1 store 6.0.

Input/Output

Meaning
Output such as print sends a value to the screen for a person to read, and the program cannot reuse it.
Key test
A person sees it; no variable receives it.
Example
print(half(10)) shows 5.0 on screen, but nothing in the program keeps that 5.0.

Parameters

Meaning
Parameters are the named inputs in a function's definition that receive values each time it is called.
Key test
The value travels into the function at the call, not out of it.
Example
In def half(n), n is the parameter, and half(10) puts 10 into n.

Formula & Notation

Worked Examples

Example 1 — Trace returns inside an expression

Easy

A function is defined as def area(w, h): return w * h. Trace total = area(3, 4) + area(2, 5) and give the value of total.
Show the steps and the answer
  1. Call area(3, 4): w = 3 and h = 4, so return sends back 3 × 4 = 12.

    The arguments fill the parameters in order, then the return line computes and hands back the result.

  2. Replace the call with its value: the line is now total = 12 + area(2, 5).

    A call that returns a value can be treated exactly like that value in the expression.

  3. Call area(2, 5): return sends back 2 × 5 = 10.

    Each call runs the function fresh, with its own w and h.

  4. Finish: total = 12 + 10 = 22.

    The addition and the assignment can happen only after both calls have returned.

Answer: total = 22.

Takeaway: Trace a call by running the function, then swapping the call for the value it returns.

Example 2 — Code after return never runs

Standard

A function grade(score) has three lines in its body: (1) if score >= 50: return 'pass', (2) return 'fail', (3) print('graded'). What does grade(70) return, what does grade(30) return, and is 'graded' ever printed?
Show the steps and the answer
  1. grade(70): line 1 tests 70 >= 50, which is True, so return 'pass' runs.

    return sends 'pass' back and ends the call at once, so lines 2 and 3 are skipped.

  2. grade(30): 30 >= 50 is False, so line 1's return is skipped and line 2 returns 'fail'.

    A function can contain several return statements; the first one reached is the one that counts.

  3. Line 3, print('graded'), comes after a return on every path, so it never runs.

    Every path has already left the function by line 2, which makes line 3 unreachable.

  4. To print and also return, move print('graded') above line 1.

    A line runs only if control reaches it before a return does.

Answer: grade(70) returns 'pass', grade(30) returns 'fail', and 'graded' is never printed.

Takeaway: return ends the function immediately; anything after it on the same path is dead code.

Example 3 — Find the mistake: print is not return

Application

Zara writes def double(x): print(x * 2) and then answer = double(4) + 1. The screen shows 8, and then the program crashes. Zara says: "The function works, because it showed 8." Find the mistake and correct it.
Show the steps and the answer
  1. Trace double(4): print(x * 2) shows 8 on the screen.

    print sends text to the screen for a person to read; the calling code receives none of it.

  2. The function ends without a return statement, so in Python the call gives back None.

    A function that never reaches return hands back the special value None.

  3. answer = None + 1 raises a TypeError, which is the crash Zara saw.

    None is not a number, so it cannot be added to 1.

  4. Fix: def double(x): return x * 2. Now double(4) is 8, and answer = 8 + 1 = 9.

    return hands the value to the caller, where the calculation can use it.

Answer: Replace print(x * 2) with return x * 2; then answer = 9.

Takeaway: Seeing a value on the screen does not mean the caller received it; use return whenever other code needs the result.

Where You'll See It

  • A vending machine

    You put in money and a code (the arguments) and the machine hands back a snack (the return value) — and once it drops the snack, it stops, just like return exits a function.

  • Asking a calculator app for a tip

    A calc_tip(bill) function does the math and returns the dollar amount so your code can display it; if it only printed the number, the rest of the program couldn't use it in the total.

  • Stacking helpers like nesting dolls

    double(triple(2)) works because triple returns 6 and that returned 6 becomes the input to double, giving 12 — return values let functions feed each other.

Common Mistakes

  1. The slip: print(value) and return value do the same thing.

    Why it's tempting: Both seem to 'output' the value, and in the console you see the number either way.

    Instead: print only shows text on screen; return hands the value back to the caller. A function that prints but doesn't return gives None to whoever called it.

  2. The slip: Code written after a return still runs as cleanup.

    Why it's tempting: It's physically below the return in the file, so it looks like the next step.

    Instead: return immediately exits the function, so any code after it on the same path is unreachable and never executes.

  3. The slip: Calling a function automatically saves its result.

    Why it's tempting: The function clearly computed an answer, so the answer feels like it must be kept.

    Instead: The return value is lost unless you capture it, e.g. result = compute(x). Just writing compute(x) on its own line throws the value away.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. def double(n): return n * 2. What is double(double(3))?

    Hint: Work from the inside out.

  2. def area(w, h): return w * h. What is total after total = area(3, 4) + area(2, 5)?

    Hint: Each call is replaced by the value it returns.

  3. check(n) returns 'big' if n > 10, otherwise it returns 'small'. A line print('done') comes after both returns. What do check(15) and check(4) return, and is 'done' ever printed?

    Hint: What does return do to the rest of the function?

  4. def square(n): return n * n. The program runs square(6) on one line, then print(answer) on the next. What goes wrong, and how do you fix it?

    Hint: Where did the returned 36 go?

  5. average(scores) must show the average on screen and also feed it into a grade calculation later. Should the function print the value or return it?

    Hint: Can the grade calculation use something that was only printed?

  6. A student writes def add(a, b): print(a + b), then total = add(2, 3) * 10, and expects total to be 50. What really happens?

    Hint: What does a function give back if it has no return statement?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Can a function return more than one value?

Strictly it returns one thing, but that thing can hold several values. In Python, return low, high sends back a pair called a tuple, and the caller can unpack it with a, b = min_max(scores). In Java you would return an array or a small object instead. If a function is returning five unrelated things, it is usually doing too many jobs and should be split.

Why does print(print('hi')) show hi and then None?

The inner print runs first and shows hi. Like any call, it is then replaced by its return value, and print returns None, Python's value for nothing. The outer print then shows that None. The puzzle shows the difference in one line: showing text is something a function does along the way, while the return value is what the call turns into.

My search function only ever checks the first name. Why?

Look for a return in the wrong place. In a loop that checks each name, return True inside the if is right, because finding a match ends the search. But if return False sits inside the loop as well, the function ends after the first name that does not match. Move return False below the loop, so it runs only after every name has been checked.

Should a checking function return True or False, or print 'yes'?

Return the Boolean. An is_adult(age) that returns True or False can be used in an if, in a count, or inside another function, while one that prints 'yes' can only be read by a person. Keep printing in one place, usually the main part of the program, and let helper functions return values. That also makes them easy to test, because a test can check a returned value directly.

Learning Path

Return Values

You are here

Next →

Scope
You need Function (Programming), how to define a function and call it with arguments, first. Return values add the way a function hands its result back, and the rule that return ends the function. Next, Scope explains why a function's local variables vanish when it returns, which is exactly why a result has to leave through return.

See Also