Skip to main content

Getting results from functions

So far, our functions have printed output. But often you want functions to calculate something and give you the result to use elsewhere.

The return statement

Use return to send a value back from a function:
When Python hits a return statement, it immediately exits the function. Any code after return won’t run.

Using returned values

Returned values can be used in many ways:

Returning multiple values

Python can return multiple values as a tuple:

Return vs print

Understanding the difference is crucial:
Use return when you need to use the result elsewhere. Use print when you just want to display information.

Functions without return

Functions without explicit return statements return None:

Common mistakes

What’s next?

Now that you know the basics of functions, let’s look at how to handle arguments flexibly using variable-length and special parameters.

Flexible Argument Handling

Master *args, **kwargs, positional-only, and keyword-only parameters