---
title: "Return Values and Variable Scope"
description: "Sending a result back out of a function, and the rule that a name created inside a function does not exist outside it."
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-09-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

Sending a result back out of a function, and the rule that a name created inside a function does not exist outside it.

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 a variable made inside a function cannot be printed after it ends
- Tell apart a function that prints from a function that returns

## Lesson: Return it, do not just show it

A return sends a value back to the caller for later use. calcScore takes a count correct and returns the score, so sallyScore = calcScore(27) stores 90. Print only shows, return delivers.

**Example.** Returned values plug into bigger expressions, like printing Sally earned: beside calcScore(27). Built-ins work the same way: Math.max picks the bigger of two numbers, and substr slices Harry out of Harry Potter. Each hands something back.

Return ends the function on the spot, so put it last. In calcLineSlope the return sits above the slope maths, which never runs and hands back an undefined value. Code after a return is dead.

**Tip.** Names made inside a call vanish when it ends, so share data through returns instead. Take inputs in, send outputs out, and your pieces stay predictable.

**Recap.** Return answers to the caller, keep returns last, and let locals live and die inside their call.

## 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)
