What is the purpose of the return statement? How is it different from printing?

output, python, return

Solution

The `print()` function writes, i.e., "prints", a string in the console. The `return` statement causes your function to exit and hand back a value to its caller. The point of functions in general is to take in inputs and return something. The `return` statement is used when a function is ready to return a value to its caller.

For example, here's a function utilizing both `print()` and `return`:

def foo():
    print("hello from inside of foo")
    return 1

Now you can run code that calls foo, like so:

if __name__ == '__main__':
    print("going to call foo")
    x = foo()
    print("called foo")
    print("foo returned " + str(x))

If you run this as a script (e.g. a `.py` file) as opposed to in the Python interpreter, you will get the following output:

going to call foo
hello from inside foo
called foo   
foo returned 1

I hope this makes it clearer. The interpreter writes return values to the console so I can see why somebody could be confused.

Here's another example from the interpreter that demonstrates that:

>>> def foo():
...     print("hello within foo")
...     return 1
...
>>> foo()
hello within foo
1
>>> def bar():
...   return 10 * foo()
...
>>> bar()
hello within foo
10

You can see that when `foo()` is called from `bar()`, 1 isn't written to the console. Instead it is used to calculate the value returned from `bar()`.

`print()` is a function that causes a side effect (it writes a string in the console), but execution resumes with the next statement. `return` causes the function to stop executing and hand a value back to whatever called it.

Problem

What does the `return` statement do? How should it be used in Python? How does `return` differ from `print`? See also Often, people try to use `print` in a loop inside a function in order to see multiple values, and want to be able to use the results from outside. They need to be returned, but `return` exits the function the first time. See How can I use `return` to get back multiple values from a loop? Can I put them in a list?. Often, beginners will write a function that ultimately `print`s something rather than `return`ing it, and then also try to `print` the result, resulting in an unexpected `None`. See Why is "None" printed after my function's output?. Occasionally in 3.x, people try to assign the result of `print` to a name, or use it in another expression, like `input(print('prompt:'))`. In 3.x, `print` is a function, so this is not a syntax error, but it returns `None` rather than what was displayed. See Why does the print function return None?. Occasionally, people write code that tries to `print` the result from a recursive call, rather than `return`ing it properly. Just as if the function were merely called, this does not work to propagate the value back through the recursion. See Why does my recursive function return None?. Consider How do I get a result (output) from a function? How can I use the result later? for questions that are simply about how to use `return`, without considering `print`.

Original source

Related problems