LightMySky
En anglais

Return Values and Variable Scope

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

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • 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

1 · Read

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.

Try it together

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.

ticket_total(4)count = 4return 12caller: cost = 12
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.

inside: total = 14return totalcaller: saved = 14

The caller receives the value, not access to every local name.

Try it together

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.

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.

Readings and flashcards about Python results, local names and return order independently written for this dataset.

More to read

Who uses a function's result?

A parcel-price function calculates a number. Its caller might add that price to another charge, store it in an order, or display it. Returning the number leaves those choices with the caller.

In Python, return price evaluates price and sends its value back. A caller can write charge = parcel_price(weight) to store the result under its own name. Returning the number does not display it. A print call can display it when needed.

A function can also print a progress message before returning its result. These are separate actions: printing produces output, while returning supplies the result of the call.

If the function reaches its end without executing return, Python returns None. A bare return also returns None. None is different from zero and from any number the function happened to print. Assigning the result of a printing-only function therefore does not capture the displayed number.

A local name is not a caller name

In the ordinary Python functions used here, parameters and locally assigned names belong to the function. Calling the function does not create those names in the caller.

Suppose a function assigns a local price and returns it. The caller can store the result in charge. The caller now has charge; this does not give it access to the function's local price. If the caller already had its own price, that would be a separate binding. If it had no price, trying to read that outside name would raise an error.

Scope describes access to names, not automatic destruction of every object when a call ends. An object returned by the function can remain available through the caller's binding.

Each call supplies values for its own parameters. A local value from an earlier completed call is not automatically the next call's local value. Pass needed information as arguments or return it for the caller to keep.

Check the return and the work before it

Trace a function in execution order. First identify the input values. Then follow the statements that calculate the result. When the return is reached, evaluate its expression and pass that value to the caller.

In the ordinary functions shown in this topic, later body statements are skipped after return. If the returned expression needs a locally calculated value, calculate that value first. Returning an unassigned local name raises an error instead of producing the intended result.

Return does not have to be the last written line in every function. Its position must match the intended execution path. The examples here focus on straight-line bodies so you can trace the order directly.

Separating a calculation from display often helps testing: compare the returned value with an expected value without reading screen output. Printing is still useful when display is the function's job.

A global name in Python belongs to a module's scope, not to every part of every program. Passing arguments and returning results can reduce reliance on shared state, but they are not the only valid ways for program parts to communicate.

2 · Watch

En anglais

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Then practise

8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Return Values and Variable Scope · Computing, ages 15 to 16 · LightMySky