Return Values Examples in CS Thinking
Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Return Values.
This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.
Concept Recap
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.
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.
Read the full concept explanation โHow to Use These Examples
- Read the first worked example with the solution open so the structure is clear.
- Try the practice problems before revealing each solution.
- Use the related concepts and background knowledge badges if you feel stuck.
What to Focus On
Core idea: Return values let functions communicate results back to their callers. A function without a return value just performs an action.
Common stuck point: Print and return are different. Print displays a value; return sends it back to the calling code for further use.
Sense of Study hint: 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.
Worked Examples
Example 1
mediumAnswer
First step
See the full worked solution + why-it-works coaching
SetupKey insightWhy it worksCommon pitfallConnection
Example 2
mediumExample 3
mediumExample 4
mediumExample 5
hardExample 6
hardPractice Problems
Try these problems on your own first, then open the solution to compare your method.
Example 1
mediumExample 2
mediumExample 3
easyExample 4
easyExample 5
easyExample 6
easyExample 7
easyExample 8
easyExample 9
easyExample 10
easyExample 11
mediumExample 12
mediumExample 13
mediumExample 14
mediumExample 15
mediumExample 16
mediumExample 17
mediumExample 18
mediumExample 19
mediumExample 20
challengeExample 21
challengeExample 22
challengeExample 23
easyExample 24
easyExample 25
easyExample 26
easyExample 27
easyExample 28
easyExample 29
mediumExample 30
mediumExample 31
mediumExample 32
mediumExample 33
mediumExample 34
mediumExample 35
mediumExample 36
mediumExample 37
mediumExample 38
hardExample 39
hardExample 40
hardExample 41
challengeRelated Concepts
Background Knowledge
These ideas may be useful before you work through the harder examples.