loop
Function return

Module 4 · Function return · Lesson 1 of 4

Effects and results: return

A function can do two quite different things. It can have an effect — something visible that happens, like print putting text on the screen. And it can produce a result — a value it hands back to whoever called it. The tool for handing a value back is return.

The call becomes its returned value
result =square(5)
def square(n):
returnn * n25
↑ returns 25
result?

Here's the key idea: return makes the whole call become that value. After return n * n runs, square(5) is, for all purposes, just 25 — so you can store it, print it, or drop it straight into another expression.

Run it yourselfruns in your browser · Python
main.py

One more thing return does: it ends the function immediately. The moment Python hits a return, it stops and jumps back to the caller — any lines below it in the function are skipped. That makes it perfect for handling special cases early.

Run it yourselfruns in your browser · Python
main.py

So a function can act, return a value, or both. But what does a function give back when it has no return at all? Python has a special answer for that — next.