---
title: "Return Values and Variable Scope"
description: "Using a function's returned result, and tracing which names are local to an ordinary Python function."
canonical: https://lightmysky.com/learn/computing/return-values-and-variable-scope-mt_jivtTHpZc7
source: https://lightmysky.com/learn/computing/return-values-and-variable-scope-mt_jivtTHpZc7.md
retrieved: 2026-11-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Return Values and Variable Scope

Using a function's returned result, and tracing which names are local to an ordinary Python function.

Subject: Computing · Area: Programming · Ages 15 to 16
Page: https://lightmysky.com/learn/computing/return-values-and-variable-scope-mt_jivtTHpZc7

## Ready when they can

- Return a value and store it in a variable where the call happened
- Explain why assigning a local variable does not create that name in the caller
- Tell apart a function that prints from a function that returns

## Lesson: Return values and local names

You have defined Python functions with parameters. A return sends a result to the code that called the function. That code can store the result or use it in another expression. print displays output; it does not return the displayed value. A function can print a message and also return a value.

**Example.** Tickets cost 3 coins each. Define a Python function ticket_total(count) whose only body statement is return count * 3. The call ticket_total(4) passes 4 into count and returns 12. Writing cost = ticket_total(4) stores that returned number in cost. Then print(cost) displays 12, while cost + 2 uses the stored number in a calculation.

*(drawing: The argument goes into the function. The returned number goes back to the caller.)*

In the ordinary functions shown here, return ends the current call and execution continues in the caller. Later statements in that function are skipped. A return need not be the last written line, but calculations needed for its result must happen before it. In Python, a return with no expression, or reaching the function's end without return, gives the caller None.

A parameter or a name assigned locally inside an ordinary Python function does not become a name in its caller. If total is assigned only inside the function, outside code cannot read that local total. With no outside total, print(total) raises an error. An outside total, if one exists, is a separate binding. Returning a value lets the caller store it under its own name.

**Example.** An ordinary Python function get_total() has two body statements, in this order: return total; total = 14. The assignment makes total local, but return tries to read it before assignment. The call raises an error. Put total = 14 before return total so the call returns 14.

When several later calculations need one computed value, return it and store the call's result once. Each calculation can then read that stored value. This avoids copying numbers from screen output or relying on a shared global variable. Printing remains useful for displaying a message or result. Choose the function's job to match what its caller needs.

**Recap.** Return a result to the caller. Calculate it before returning. Local names do not become caller names; store returned values for later use.

*Python lesson, questions, diagrams and flashcards independently written for this dataset.*

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Defining a Function with Parameters](https://lightmysky.com/learn/computing/defining-a-function-with-parameters-mt_pyo7yBg4H3)

## Opens up

- [Semantic Analysis and the Intermediate Representation](https://lightmysky.com/learn/computing/semantic-analysis-and-the-intermediate-representation-mt_f2z_WQxfiw)
- [Decomposing a Problem into Subprograms](https://lightmysky.com/learn/computing/decomposing-a-problem-into-subprograms-mt_P293LckCnd)
- [Functions at Depth: Default and Keyword Arguments](https://lightmysky.com/learn/computing/functions-at-depth-default-and-keyword-arguments-mt_SR8RhRmYhz)
- [Recursion and the Call Stack](https://lightmysky.com/learn/computing/recursion-and-the-call-stack-mt_VGjfeF3F2e)
