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 the basics of functions, let’s look at how to handle arguments flexibly using variable-length and special parameters.Flexible Argument Handling
Master *args, **kwargs, positional-only, and keyword-only parameters