Return Values
def add(a, b):
return a + b
Often, a function will take its arguments, do some computation, then return a value to be used where the function was called. The return
keyword is used for this. In our example, the statement return a + b
is called the return statement. It ends the function execution and 'sends back' the result of the function.
It is common to say that a function “takes” arguments and “returns” a result.
Note that some functions do not necessarily return a result.
You can visualize a function as a machine that takes in arguments, and spits out the return value:
Concretely, for the add
function that we defined and called like this:
def add(a,b):
return a + b
add(3,5)
Programs can use the return value for further operations:
result = add(3,5)
print("the result was", result) # the result was 8
bigger_result = add(result, 10)
print("the bigger result was", bigger_result) # the bigger result was 18
If you feel a bit confused, don’t worry. This is a new concept, and it takes a while to get used to. Watch the video below for a more detailed explanation.
Return vs. Print
print
is different from returning a value. If you use print
, a message shows up in the console, but the rest of the program cannot access this message.
def add_and_print(a,b):
print(a + b)
result = add_and_print(3,5) # 8
print("the result was", result) # the result was None
The print
in the function will print out the value, but it will not return it. None
is the value that functions return by default. If they don’t explicitly return something else, they return None
.
Practice
Types of return values
Functions return different types of values. Here is an example of a function that checks if a given username is valid or not. It returns a boolean:
def is_valid(username):
if len(username) > 5: #checks to see if username has more than 6 characters
return True
else:
return False
print(is_valid('user')) # will print False
print(is_valid('mhassan')) # will print True
You can return any of the types you’ve learned so far from a function - numbers, strings, booleans, or lists.