Return Statement in Python

Return Statement in Python

We can have functions that doesn’t return anything: no value and no object. We don’t need that. But we can make our function return anything.
def korek(a,b):

if a > b:

return a

else:

return b

We see the result:
>>> korek(3,6)
6
>>>

>>> korek(6,3)
6
>>>

Even if we didn’t see the return statement, every function included it by default at the end of its code. We can check that be using print:

print(func_name)

>>> def koreczek():
…     pass

>>> print(koreczek())
None
>>>

A return statement in a function passes back an expression to the caller (the place where the function was called).  It is the same as return None without arguments

Let’s look at the function:

def korek()

print(“Printing a string”)

return 2+ 5

And now we should see the result:

>>> korek()
Printing a string
7
>>>

It looks as though the statement return does the same as print. However, it is not the same as print, because the latter writes a string to our console. In other words, return gives something back or replies to the caller of the function while print produces only text.

>>> def korek():
…     print(“I’m here!”)
…     return 298

>>> korek()
I’m here!
298
>>> def korki():
…     return 128 + korek()

>>> korki()
I’m here!
426
>>>

So we see that when korek() is called from korki(), 298 is added to 128, and the sum is returned to the console. And we have the side effect: the printing “I’m here!”.

It’s worth remembering that the return statement does two things:

— it finishes a function — there can be more code after the return, but the function ends because the interpreter has met the statement return, for example:

def korek(a,b,c)

return a

return b

return c

After running:

>>> korek(1,2,3)
1
>>>

So only the first return statement has worked, and the program has ended.

— it passes date back from a function to a caller

If the control goes outside of our function without running the return statement, we will get None. The return statement can only be inside a function definition.

More real example:

def wieczko(a,b):

sum = a+b

print(“We have inside the function: “, sum)

return sum

>>> sum = wieczko(234,567)
We have inside the function:  801
>>> print(“We have outside the function: “, sum)
We have outside the function:  801
>>>

In this example, there would be None without the return statement. We couldn’t have access to the returned value of our function from outside; only from the caller. In other words, the return statement sends the returned value outside the function, and we can do with it what only we want.

The syntax of the return statement

return expression

Yes, it can be not only an integer but also an expression. It’s evaluated, and the value of that is passing to a caller.

We can keep the returned value in a variable.

var = def korek(a,b)

>>> var = korek(78,345)
>>> var
345
>>>

Our function can return not only one value but more. We can use a tuple to get all these values. We may have in the function body such a statement:

return expression1, expression2, expression3…

To get their values, we should write:

var1, var2, var3… = func()

After running:

var1 = expression1

var2 = exression2

var3 = expression3…

The number of variables must be the same as the number of expressions to get all their values. if not, we’ll get ValueError. The values can be objects of different types.

def tok(a,b,c):

return a, b, c

>>> var1,var2,var3 = tok(1,2,3)
>>> print(var1,var2,var3)
1 2 3
>>>

It works!

Leave a comment