Again... Experimenting with return
but-in Python
Examples
As usual, let's dive into a bit of experimentation to better understand return
, but this time with Python. (I felt uncomfortable with Perl as I mentioned in the previous post.) Anyway, back to Python! In Python, the return
keyword is used to stop a function’s execution and give back a value from that function. But there are a few things worth understanding about how it works. For example, here’s a function where we want to add two numbers and print the result:
In this function, main
takes in two parameters, a
and b
, adds them together, then returns the result with return c
. After the return
, there’s a print(c)
, which should print out c
. But when we run this, it doesn’t print anything. Why?
Why print
Doesn’t Work After return
When Python hits return
, it immediately stops the function’s execution. So anything that comes after return
(in this case, print(c)
) never runs. If we want c
to print, we need to move print(c)
before return
. Here’s how it looks fixed:
With this order, print(c)
gets executed, so the value of c
(37) prints before the function returns it.
Alternative: Printing the Function’s Return Value
There’s another way to print the result without changing the position of return
. Instead of using print
inside the function, we can print the return value of main
directly:
With this approach, main
just returns the value of c
, while the print
outside of it takes care of printing it.
Using print
and return
Together in One Line
Alternatively, we can write print
along with return
in a single line, like this:
In this example, print(c)
will print when the function runs. But there’s a catch: in Python, print
always returns None
. So, even though this code works for printing the result, the value returned by main
isn’t c
—it’s None
.
Best Practice Recommendation
If you want the function to both print and return the value of c
, use code like this:
Or, even simpler, you can skip return
entirely since Python automatically returns the last evaluated expression: