A function can do more than carry out instructions: it can also send a result back to the code that called it. This result is called the function's return value. Understanding return values lets you build functions whose results can be stored, displayed, or used in further calculations.
When a function finishes, control returns to the code that called it. A return statement ends the function's execution and can provide a value to the caller.
All flashcards
What is a function's return value?
The result the function sends back to the code that called it. The caller can store it, display it, or use it in another expression.
What is the difference between print and return?
`print` displays information, while `return` ends the function and makes a value available to the calling code. Printing a value does not, by itself, return that value.
What happens when a function has no return statement?
If the function finishes without executing a return statement, Python returns `None` automatically.
How can multiple return statements be used?
They can provide results for different execution paths, such as the branches of an `if`-`else` statement. Whichever return statement runs ends that function call.
Why can a function call be used in an expression?
A function call evaluates to the value the function returns. That value can be combined with compatible values, passed to another function, or stored in a variable.
Quiz questions
What does a return value represent?Answer: The result a function sends back to the code that called it.
What does a Python function return if it finishes without executing a return statement?Answer: It returns `None` by default.
What happens when a function executes `return value`?Answer: The function ends and sends `value` back to its caller.
If `square(number)` returns `number * number`, what does `square(3) + 4` evaluate to?Answer: 13
What is the difference between `print(value)` and `return value` inside a function?Answer: `print` displays the value; `return` makes it available to the caller.
Based on Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield, Narges Norouzi (OpenStax), licensed CC BY 4.0. Adapted into a summary, flashcards and quizzes.