What does return do?
It sounds silly, but
return does literally what it says – it returns something to wherever the function was called in the program. If we consider the
square() function in this exercise, for example, it does some calculation with the number given to the function as
n, and then
returns the final result called
This allows us to do lots of useful things! When we
return a value it can be stored in a variable for later use, or used on the spot, or any number of things. It basically makes your function call a usable value!
Take a look at the code below for an example:
def square(n): """Returns the square of a number.""" squared = n ** 2 return squared my_number_squared = square(10) print my_number_squared # Prints 100 print square(10) # Also prints 100!
We not only used the returned value to store a value in a variable, but we also used it with a built in method that takes a value and prints it,