Code Bytes · "Intro to Programming" · Episode 13 · Functions and lists
Parameters and return values
A parameter is a named slot in a function's brackets that receives whatever value you hand in. return sends a value back out of the function, so your code can store it or keep using it. print only shows a value on screen.
Ingredients in, dish out
Last episode, our function greet() always did exactly the same thing. Useful, but a bit limited. Most real functions work more like a recipe: ingredients go in, and a dish comes out.
- What goes in are values you hand to the function.
- What comes out is the result the function hands back.
Those two ideas have names: parameters (in) and return values (out).
Parameters: a named slot for what you hand in
``python def double(n): return n * 2 ``
The n inside the brackets is a parameter. Think of it as an empty, labelled slot. When the function runs, whatever value you hand in drops into that slot, and inside the function you use it by its name, n.
The value you actually hand in when you call the function (like the 5 in double(5)) has its own name too: it's called an argument. So: the parameter is the slot, the argument is what goes into it.
return: sending the answer back out
The line return n * 2 does two things:
- It works out
n * 2. - It hands that value back to whoever called the function, and the function stops there.
Now the call itself turns into that value, so you can catch it in a variable or print it:
```python def double(n): return n * 2
answer = double(5) print(answer) print(double(10)) ```
`` 10 20 ``
Step by step: double(5) drops 5 into the n slot, the function works out 5 * 2, and return hands 10 back into the box called answer. print(answer) shows it. Then double(10) drops 10 into the same slot and sends back 20. Same function, different input, different output.
print shows, return hands back
This is the mix-up almost every beginner makes once. print and return can look alike, because both seem to "give you the answer". They don't do the same job:
printshows a value on the screen. Then it's gone: your code can't use it.returnhands the value back to your code, so you can store it, add to it, or pass it on.
Here's what happens if you use print where you meant return:
```python def double(n): print(n * 2)
x = double(5) print(x) ```
`` 10 None ``
The 10 is shown on screen by the print inside the function. But nothing was handed back, so x gets Python's special "nothing here" value, None. A function with no return always hands back None.
Try it yourself
Open any free online Python editor (search "online Python") and run this:
```python def greet(name): return "Hi, " + name + "!"
print(greet("Sam")) print(greet("Ana")) ```
You should see:
`` Hi, Sam! Hi, Ana! ``
One function, two different inputs, two different results. Now change it so it greets you by your own name. Then try writing def triple(n): that returns n * 3, and print triple(4) (you should get 12).
Good to know
- Every snippet on this page was run with Python 3 and printed exactly what is shown.
- Parameter is the name in the brackets of the
def(n). Argument is the value you pass in the call (5). People often mix the two words up, and that's fine while you're learning. returnends the function straight away. Any lines after it inside the function don't run.- A function with no
returnhands backNone. - A function can have several parameters, separated by commas, like
def add(a, b):. We'll use that later. - Because
double(5)turns into a value, you can use it inside bigger expressions, such asdouble(5) + 1. - Next episode: lists and arrays, how to keep many values together in one variable.
Answer to the comment question
What does this print?
``python def add_tax(price): return price + 2 print(add_tax(3) * 2) ``
It prints 10.
`` 10 ``
Work from the inside out. add_tax(3) drops 3 into the price slot, and return price + 2 hands back 5. So the last line becomes print(5 2), which shows 10. The trap is doing the 2 too early (3 * 2 = 6, plus 2 = 8). But the function call is worked out first, and only then is its result doubled.
Test your knowledge
Quick quiz: Parameters and return values
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Python Software Foundation, "More Control Flow Tools": Defining Functions: parameters go in the brackets of a def; return sends back a value; a function without return gives None
- Python Software Foundation, "Simple statements": The return statement: return leaves the function and hands back the value of its expression
- Python Software Foundation, Glossary: parameter: a parameter is the named entity in a function definition that receives the value passed in
- Python Software Foundation, Glossary: argument: an argument is the value passed to a function when it is called
Video transcript
What goes into a function, and what comes out?
Think of a recipe. Ingredients go in, and a dish comes out. Functions work the same way.
We type def double, and in the brackets, n. That n is a parameter: a named slot for whatever you hand in.
Inside: return n times two. Return sends the answer back out of the function.
Now: answer equals double of five. Five drops into the n slot, and ten comes back into the answer box.
Print answer: ten. Hand it ten instead, and it sends back twenty. Same function, different input.
Print just shows a value on screen. Return hands it back, so your code can keep using it.
What does this print? Comment your answer below!
That's your byte.
More from "Intro to Programming"
- "Intro to Programming" EP10While loops
- "Intro to Programming" EP11Infinite loops
- "Intro to Programming" EP15Indexing starts at zero
- "Intro to Programming" EP16Reading an error message