Beginner Topic 14 of 54 Programming Fundamentals
Return Values
The result a function hands back to its caller.
How it works
A return statement ends the function immediately and hands a value back to the caller, whose expression is then replaced by that value. Functions can return several values through tuples or objects, or nothing at all, shown as void or None. Return values can also encode failure through error codes, optional types or result types.
Step-by-step walkthrough
- The caller passes x = 4.
- The function computes x * x = 16.
- return 16 hands the value back and ends the function.
- The caller stores it, for example y = 16.
- A function without return gives back nothing, such as None or void.
Where it is used
Computed results, status codes, found-or-missing values and newly built objects are all delivered by returning. Guard clauses use early returns to leave a function as soon as a special case is handled.
Common pitfall
Forgetting to return on some code path yields null or None unexpectedly. Returning different types on different paths confuses callers. Ignoring a returned error code lets failures pass unnoticed, and returning references to internal mutable state breaks encapsulation. Keep all paths consistent.
Where it fits in the course
Return Values is topic 14 of 54 in Programming Fundamentals, rated Beginner. It follows Parameters and Arguments. The next topic is Scope.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Programming Fundamentals quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Programming Fundamentals quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Return Values?
Where do people use Return Values?
What is the most common mistake with Return Values?
What should I learn before this topic?
How can I practice it?
Is Return Values hard to learn?
How long does it take to learn Return Values?
Which topics are related to Return Values?
What should I study after Return Values?
Related topics
- Parameters and Arguments (Programming Fundamentals)
- Scope (Programming Fundamentals)
- Functions (Programming Fundamentals)
- Comments and Documentation (Programming Fundamentals)
- Return-Oriented Programming (Security & Cryptography)