In this tutorial you’ll learn more about functions in Python. In particular, we will focus on the return keywword. This keyword is used to exit a function and return (as the word suggests) a specific value.
Return Syntax
Let’s create a basic function to perform an addition. The result of the addition will be our return value (the output).
def addition(x, y):
result = x + y
return result
Instead of simply printing the result, in this example, we define it as the output of the function, using return. Now, how can we print the function’s output?
# ... copy the function above result = addition(3, 4) print(result) # Output: 7
At line 3, we call our function, passing two numbers (i.e., 3 and 4) to it. The output of the function (i.e., 7) is then stored in the result variable, which is then printed.
The return