Python @ DjangoSpin

Python: Returning values from a function - The return keyword

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

return in Python

return in Python

The return statement makes the control exit from a function, with an optional expression.

>>> def calculateArea(length, breadth):
    return length * breadth
 
>>> calculateArea(3, 4)
12
 
 
 
>>> def myMaxFunction(num1, num2):
    if num1 > num2:
        return num1
    else:
        return num2
 
     
>>> myMaxFunction(10, 20)
20
 
>>> maxValue = myMaxFunction(20, 90)
>>> print("Max Value:", maxValue)
Max Value: 90
 
 
>>> def isOdd(number):
    if number % 2 == 1:
        return True
    else:
        return False
 
     
>>> isOdd(5)
True
>>> isOdd(6)
False

A return statement without an expression is as good as return None.

Functions can return anything, from a string to an integer to a Boolean value to lists to sets and so on. In fact, they can even return other functions. I’ll leave that to you to explore.

Functions can return multiple values as well.


See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply