Values In, One Value Out
Parameters carry values into a function and the return statement hands one value back out to the caller. · 11 min
A function that always does the same thing is useful only once. The power comes when you can feed it different values each call and get an answer back you can use. Two mechanisms make that possible. A parameter is a name in the definition that stands for a value you pass in. The return statement hands exactly one value back out to whoever called the function. Values go in; one value comes out.
Guess before you learn
This function is meant to give back a doubled number so you can reuse it. What does print(result) show?
``
def double(n):
print(n * 2)
result = double(5)
print(result)
``
It shows None. The body prints 10 as a side effect, but the function never returns anything — so it hands back the value None, and that is what lands in result. If you expected 10, keep that pencil mark: telling return apart from print is the exact knot this folio unties.
9–12
3–5
A parameter is a blank the function fills with whatever you hand it. Call double(5) and the blank n becomes 5. Inside, the function works out an answer and uses return to hand exactly one value back to you. You put a value in; one value comes out.
6–8
A parameter is a name that stands for a value you pass in when you call. In def double(n):, n is the parameter; in the call double(5), the value 5 is the argument that fills it. Inside the body, return sends exactly one value back to the caller and ends the function at once. The returned value takes the place of the call, so result = double(5) stores 10 in result. Values go in through parameters; one value comes out through return.
9–12
A function's parameters are the local names listed in its definition; the arguments are the actual values supplied at the call, matched to parameters in order. return does two things at once: it fixes the single value the call evaluates to, and it stops the function on the spot — any lines after it are skipped. A function with no return still returns a value: the special value None. Keeping return (hand a value back) apart from print (show text on screen) is the single most common thing beginners have to untangle here.
K–2
A snack machine takes your coins and gives back one snack. You put something in; you get one thing out. A function can work the same way: give it a number, and it hands one answer back to you.
Undergrad
Parameters are bound in a fresh local namespace on each call; Python passes references by assignment, so a parameter name is rebound to the argument object. return supplies the value of the call expression and transfers control back to the caller; reaching the end of the body without a return yields None. A function that returns a value is composable — its result can feed directly into a larger expression — whereas one that only prints performs a side effect and evaluates to None. Preferring return over print is what makes functions building blocks rather than dead ends.
Postgrad
Arguments are evaluated in the caller's environment, then bound to parameter names in the callee's activation record; return e evaluates e in that record and installs the result as the value delivered to the call site's continuation, discarding the frame. A missing return is sugar for return None. The split between a value-returning function and an effectful printing one is the boundary between expression-oriented composition and statement-oriented sequencing — the former substitutes into larger expressions and is far easier to test, since a test compares a returned value rather than capturing a stream.
parameter
A name in a function's definition that stands for a value passed in. The value supplied at the call is the argument that fills it.
Why is this true?
Why can a line written after a return inside a function never run?
Because return hands a value back to the caller and ends the function at that instant. Control leaves the body immediately, so any lines below the executed return are skipped. A return is both an answer and an exit.
Trace a function with a parameter and a return — the steps fade as you master them
price = 20
tax = 20 * 0.10
total = 20 + 2
return total hands one value back. What does cost = add_tax(20) store in cost?cost = add_tax(20)
The knot to untie is return versus print. Both can show a number, but they do very different jobs. print puts text on the screen and gives the caller nothing usable — the function still hands back None. return hands the value itself back, so the caller can store it, add it, or pass it on. A returned value is something the rest of your program can build on; a printed one is just a mark on the screen.
So a function takes values in through its parameters and hands one value back with return, and return also ends the function. That is enough to write real building blocks. Next folio looks inside the call: the names a function makes are private to it, which is what lets you split a big problem into small functions that cannot trip over each other's variables.
Practice — new ink and old, interleaved
1.Evaluate 2 + 6 / 2 under Python's precedence rules.
2.Match each operator to its meaning.
3.What does this print?
``
for i in range(3):
print(i)
``
4.What is not (4 > 2)?
5.How many lines does this print?
``
def line3():
print('x')
print('y')
print('z')
line3()
``
6.Which grouping does Python use for not True and False?
7.What does result hold?
``
def square(n):
return n * n
result = square(9)
``
8.How many lines does this print?
``
def beep():
print('b')
beep()
beep()
beep()
``
9.What prints, in order?
``
def tag():
print('mid')
print('a')
tag()
print('b')
``
10.What does this print?
``
def area(w, h):
return w * h
print(area(3, 4))
``