Returning values
from functions
§ A
function which wants to return some result after performing the task such type
method is known as return type function.
§ Python
functions can return values of any type via the return keyword
§ Functions
can return a value that you can use directly.
def give_me_ten():
return 10
#Now let’s call the defined
give_me_ten() function:
ten=give_me_ten()
print(ten)
# OR let’s call the defined give_me_ten() function with-in print function:
print(give_me_ten())
or use the value
for any operations:
def give_me_ten():
return 10
#Now let’s call the defined
give_me_ten() function:
print(give_me_ten()+20)
§
If return is encountered in the function the function will be exited immediately
and subsequent operations will not be evaluated
def give_me_ten():
return 10
print('This statement will not be
printed. Ever.')
#Now let’s call the defined give_me_ten() function:
print(give_me_ten())
§
You can also return multiple values (in the form of a tuple):
def give_me_two_ten():
return 10,10 # Returns two 10
#Now let’s call the defined give_me_ten() function:
first, second =
give_me_two_ten()
print(first) # Out: 5
print(second) # Out: 5
§ A
function with no return statement implicitly returns None.
§ Similarly,
a function with a return statement, but no return value or variable returns
None
Post a Comment