What does return mean in Python? [closed]

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?


Solution 1:

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.

Solution 2:

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