Return Values

Programming Fundamentals
definition

Also known as: return, function output

Grade 6-8

View on concept map

The 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

def double(x): return x * 2. Then result = double(5) gives result = 10. The function returned 10.

๐ŸŒŸ 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

A function f with return type R terminates by executing \text{return } v where v \in R. The call expression f(a_1, \ldots, a_n) evaluates to v, allowing composition: g(f(x)).

๐Ÿšง 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.