Return Statements
You'll learn how functions send values back to the code that called them using the return statement.
What Does "Return" Mean?
When you call a function, it performs some work—but often you want it to give you back a result. The return statement is how a function sends a value back to its caller.
Think of it like ordering food at a restaurant: you place your order (call the function), the kitchen prepares it (function executes), and the server brings your meal back to you (return statement delivers the result). Without the return, the kitchen would do the work but you'd never get your food!
How Return Statements Work
A return statement does two things:
- Immediately exits the function – no code after the return runs
- Sends a value back to wherever the function was called
When you write something like:
result = calculateSum(5, 3)
The calculateSum function must use a return statement to send the answer (8) back, so it can be stored in result.
Functions Without Return
Not all functions need to return a value. Some just perform actions (like printing to the screen). Functions without a return statement simply exit when they reach the end of their code block.
Key Takeaway: The return statement is how functions deliver their computed results back to the caller, completing the two-way communication between the calling code and the function.