Return Keyword in Python Functions

Why is “return 123” ignored in this case?

def boring_function():
    print("'Boredom Mode' ON.")
    return 123

print("This lesson is interesting!")
boring_function()
print("This lesson is boring...")

This lesson is interesting!
‘Boredom Mode’ ON.
This lesson is boring…

And, why is the whole function ignored in this another case:

def boring_function():
    return 123
    print("'Boredom Mode' ON.")
    

print("This lesson is interesting!")
boring_function()
print("This lesson is boring...")

This lesson is interesting!
This lesson is boring…

Thank you.

Because you do not do anything with the result of the function.

You could print the result like this:

print("This lesson is interesting!")
print(‘function returned’, boring_function())
print("This lesson is boring...")
1 Like

Okay. I see. Thank you.

I have a follow up question… In this case, why is the print line ignored, even though it has the word “print”?

def boring_function():
    return 123
    print("'Boredom Mode' ON.")
    
boring_function()

123

Thanks.

From documentation: The return statement

return leaves the current function call with the expression list (or None ) as return value.

So anything in function body what is after return is not executed.

1 Like

I see. Thank you!

print and return have nothing to do with each other.

The purpose of print is to send text to the terminal, which will display it. (You can also tell it to “print” into a file.) It’s important to understand here that Python does not display your text - the terminal does, and it’s responsible for understanding any “special” characters like newline (to move the output onto the next line), tab (to advance to a specific position), etc. as well as for figuring out what font to use. The terminal is a very basic program that will use the same font and size for all the text that it shows. Some terminals can understand special codes that control the text colour, a few basic effects like boldface and underline, and “moving to” a position to display the next part of the text. But it will always use the same font for everything, which is controlled by the end user (configuration settings for the terminal window) and not by your program, and that font will pretty much always be a monospace (“typewriter”) font.

The purpose of a function is to compute a result, and the purpose of a function is to report that result back to the caller - which can then do anything it likes with that result. It is exactly like how using + to add two numbers gives a result that the caller can use for any purpose. It doesn’t make anything show up on the screen.

Specifically when you use the Python interpreter prompt (“REPL”), it will automatically display (in more or less the same way as print does) a string representation of the result from each statement that you type in, one at a time, if

  • the statement has a result (it’s an expression, basically meaning that you could put a = on the left-hand side of it and still have valid syntax) and
  • the result is not the special value None.

When you call print in the REPL, for example, you are using a function that will display some text as a side effect, and then return None. The REPL will see that the result was None, so it doesn’t also display that.

1 Like