LearnViz CompSci
Home / Learn / Programming Fundamentals / Return Values

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

  1. The caller passes x = 4.
  2. The function computes x * x = 16.
  3. return 16 hands the value back and ends the function.
  4. The caller stores it, for example y = 16.
  5. 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

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Programming Fundamentals quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Return Values?
The result a function hands back to its caller. It belongs to Programming Fundamentals and is a beginner-level topic.
Where do people use Return Values?
Computed results, status codes, found-or-missing values and newly built objects are all delivered by returning.
What is the most common mistake with Return Values?
Forgetting to return on some code path yields null or None unexpectedly.
What should I learn before this topic?
Start with Parameters and Arguments, the topic just before it in Programming Fundamentals.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Programming Fundamentals quiz.
Is Return Values hard to learn?
It is a Beginner topic, so it works as an entry point. If the terms are new, read the explanation twice and try the visualizer before the quiz.
How long does it take to learn Return Values?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Return Values?
Parameters and Arguments, Scope, Functions are closely connected. The full list is in the related topics section of this page.
What should I study after Return Values?
Continue with Scope, the next topic in Programming Fundamentals.

Related topics