The return statement | Python Tutorials | Vskills

The return statement

The return statement is used to return from a function i.e. break out of the function. We can optionally return a value from the function as well.


# Filename:

def maximum(x, y):
    if x > y:
        return x
    elif x == y:
        return 'The numbers are equal'
        return y

print(maximum(2, 3))


   $ python

How It Works:

The maximum function returns the maximum of the parameters, in this case the numbers supplied to the function. It uses a simpleif..else statement to find the greater value and then returns that value.

Note that a return statement without a value is equivalent to return NoneNone is a special type in Python that represents nothingness. For example, it is used to indicate that a variable has no value if it has a value of None.

Every function implicitly contains a return None statement at the end unless you have written your own return statement. You can see this by running print(someFunction()) where the function someFunction does not use the return statement such as:

def someFunction():

The pass statement is used in Python to indicate an empty block of statements.

Note – There is a built-in function called max that already implements the ‘find maximum’ functionality, so use this built-in function whenever possible.

Apply for Govt Certified Python Certification Now!!

Go back to Tutorial                                                                                Go to Home Page

Get industry recognized certification – Contact us