- Home
- /
- Computational Thinking
- /
- Programming Fundamentals
- /
- Return Values
Return Values
Also known as: return, function output
Grade 6-8
View on concept mapThe value that a function sends back to the code that called it, specified by the return statement. Return values make functions composable โ you can chain functions together: double(triple(2)) = double(6) = 12.
Definition
The value that a function sends back to the code that called it, specified by the return statement. When a function executes a return statement, it immediately stops running and passes the specified value back to the caller, where it can be stored, used in expressions, or passed to other functions.
๐ก Intuition
A function is like a vending machine โ you put in inputs (arguments) and get back an output (return value). The return value is what comes out.
๐ฏ Core Idea
Return values let functions communicate results back to their callers. A function without a return value just performs an action.
Example
๐ Why It Matters
Return values make functions composable โ you can chain functions together: double(triple(2)) = double(6) = 12. They transform functions from one-way commands into building blocks that can be combined into complex computations.
๐ญ Hint When Stuck
When writing a function, decide what result the caller needs and return that value. When calling a function, capture the return value in a variable (result = myFunction()) or use it directly in an expression. Remember that print shows text on screen but does not return a value.
Formal View
Related Concepts
๐ง Common Stuck Point
Print and return are different. Print displays a value; return sends it back to the calling code for further use.
โ ๏ธ Common Mistakes
- Using print instead of return, then wondering why the calling code receives None/undefined
- Forgetting to capture the return value in a variable, so the result is computed but immediately lost
- Placing code after a return statement, which never executes because return exits the function immediately
Frequently Asked Questions
What is Return Values in CS Thinking?
The value that a function sends back to the code that called it, specified by the return statement. When a function executes a return statement, it immediately stops running and passes the specified value back to the caller, where it can be stored, used in expressions, or passed to other functions.
When do you use Return Values?
When writing a function, decide what result the caller needs and return that value. When calling a function, capture the return value in a variable (result = myFunction()) or use it directly in an expression. Remember that print shows text on screen but does not return a value.
What do students usually get wrong about Return Values?
Print and return are different. Print displays a value; return sends it back to the calling code for further use.
Prerequisites
Next Steps
How Return Values Connects to Other Ideas
To understand return values, you should first be comfortable with function programming. Once you have a solid grasp of return values, you can move on to scope.