3

I searched the whole internet for the meaning of the return statement.

I know it ends the define statement, but I know it still does something else!

What else does it do?

Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
user3162541
  • 71
  • 1
  • 1
  • 5
  • 1
    See if this helps: http://stackoverflow.com/questions/7129285/why-would-you-use-the-return-statement-in-python – Jayanth Koushik Feb 11 '14 at 08:08
  • 3
    This question appears to be off-topic because the answer is easily found by googling "return python" – thefourtheye Feb 11 '14 at 08:08
  • 2
    In Python 2, [`return` returns a value from a function](http://docs.python.org/2/reference/simple_stmts.html#the-return-statement). That's all it does. In Python 3, it can also return values from generators. – Blender Feb 11 '14 at 08:09
  • 2
    It means the same thing it means in all other programming languages that have a return statement. This is not at all specific to python; and is also covered in just about every introductory programming tutorial that deals with functions. – l4mpi Feb 11 '14 at 08:19

2 Answers2

9

It returns the flow of control to the calling function. It also returns output/results to the calling function.

Consider the function below:

def am_i_wrong(answer):
    if answer == 'yes':
        return True
    else:
        return False

You have multiple returns. So return doesn't simply end the function definition. It instead is the point at which the function returns the result to the caller.

If answer is equal to 'yes' then anything after the if statement (after if and else) is never run because the function has already returned.

Community
  • 1
  • 1
aychedee
  • 24,871
  • 8
  • 79
  • 83
2

The docs explain fully how a return function works. Its also the first answer in the google query python return...

WeaselFox
  • 7,220
  • 8
  • 44
  • 75
  • 7
    if this guy is asking about the return statement then I don't think the docs would help. the docs seem to be a reference of python syntax to people who already know and understand python. it's not for newbies – Halcyon Abraham Ramirez Jul 29 '15 at 04:01