โ†Previous page

07FUNCTIONS ยท RETURN

A box that gives something back.

Some boxes do more than a job: they hand you a result. You send something in, it works inside, and return sends something out.

IN REAL LIFE

Something goes in, something comes out.

Many machines at home give you something back.

The juicer

Oranges go in. Juice comes out, and you can drink it.

The toaster

Bread goes in. Toast comes back to you.

The calculator

2 + 3 goes in. 5 comes back, and you can use it again.

LOOK AT THE BOX

Something goes in. Something comes out.

Give the box a name

def double(number): makes a box called double.

Send something in

What goes between the parentheses is a parameter. double(4) sends 4 into the box.

Get something out

return is the box's exit door. The result comes out through it.

4parameter
โ†’
double
โ†’
8return

From outside, you only need to know what goes in and what comes out.

OPEN THE BOX

Inside, there is nothing new.

Look inside two boxes. Every line is something you already learned.

def check_battery(battery):battery is a variable box. It gets filled when you call.
if battery < 30:An if, like in Choose a path.
plan = "charge"A variable box, like in Memory boxes.
else:The other path.
plan = "play"A variable box, like in Memory boxes.
return planreturn sends the result out of the box.
def beep(times):times is a variable box. It gets filled when you call.
for turn in range(times):A loop, like in Repeat a pattern.
print("beep")The small job that repeats.
return "done"return sends the result out of the box.

The secret: A function is just variables and instructions you already know, packed in a box with a name.

RUN IT

Use the double box.

Guess first. Then run real Python and check.

1

Guess the result

def double(number): answer = number + number return answer print(double(4))

What will Python show?

box.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide โ™ฅ

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Change 4 to 10. What comes out of the box now?

YOUR TURN

In, inside, and out.

Try each tiny question. A wrong answer becomes something to review.

0Completed
8Not tried
0Review
01

What goes in?

def greet(name):

Which word is the parameter?

02

What comes out?

def add_one(n): return n + 1 add_one(5)

What does the box give back?

03

Look inside

PyBot opened a function box.

What can it find inside?

04

Name the box

def triple(n): return n * 3

What is this box called?

05

Send in something new

def double(number): return number + number print(double(10))

What will Python show?

06

The juicer

PyBot puts 3 oranges into the juicer and gets a glass of juice.

In a function, what is the juice?

07

Fix PyBot's code

The box forgot its exit door. Make 10 come out.

Goal output
10
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

A function is a named box: in, inside, out.

Everything inside the box is something you already know. Next: boxes that already come with every value. They are called methods.