Getting results from functions
So far, our functions have printed output. But often you want functions to calculate something and give you the result to use elsewhere.The return statement
Usereturn to send a value back from a function:
When Python hits a
return statement, it immediately exits the function. Any code after return won’t run.Using returned values
Returned values can be used in many ways:Returning multiple values
Python can return multiple values as a tuple:Return vs print
Understanding the difference is crucial:Functions without return
Functions without explicitreturn statements return None:
Common mistakes
Forgetting to return
Forgetting to return
Code after return
Code after return
Printing instead of returning
Printing instead of returning
What’s next?
Now that you know functions, let’s learn how to use external libraries and APIs to extend Python’s capabilities!External tools
Use libraries and APIs