University of Free Knowledge
QA 76.73 · fol. 11

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

THE DEPTH DIAL — the same idea, younger or deeper
9–12

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.

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.

call: double(5)argument 5 fills parameter nbody computes n * 2return 1010 replaces the call in the caller
PLATE I One argument in through the parameter; one value out through return.

Trace a function with a parameter and a return — the steps fade as you master them

1
The call add_tax(20) fills the parameter price with what value?
price = 20
2
Compute tax = price * 0.10.
tax = 20 * 0.10
3
Compute total = price + tax.
total = 20 + 2
4
The line return total hands one value back. What does cost = add_tax(20) store in cost?
cost = add_tax(20)
Retrieval Gate — answer before you continue 0 / 4

1.What does this print? `` def add(a, b): return a + b print(add(4, 9)) ``

2.What does triple(4) return? `` def triple(n): return n 3 return n 100 ``

3.For def greet(name):, called as greet('Sam'), what does name hold inside the body?

4.Order what happens on the call result = square(6).

  1. The argument 6 is bound to the parameter
  2. The body runs and computes 6 * 6
  3. return hands 36 back to the caller
  4. 36 is stored in result

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.

Ink That Thinks — guess first; the answer draws itself.
The function def square(n): return n * n, called for n = 1, 2, 3, 4, 5. Place a point for the value it returns at each n — commit your guesses in pencil first.

01234560102030n passed invalue returned
Tap to place each point.
PLATE II Return values of square(n) — guess in graphite, truth in ink.
CALLN BECOMESRETURNSsquare(2)24square(3)39square(5)525
PLATE III One definition, different arguments in, different values out.
Retrieval Gate — answer before you continue 0 / 4

1.Inside a function, what is the difference between return x and print(x)?

2.What does x hold? `` def show(v): print(v) x = show(7) ``

3.What does this print? `` def half(n): return n / 2 print(half(10) + half(6)) ``

4.In one sentence, say why a function that returns its answer is more reusable than one that only prints it.

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.

==
!=
>=
not

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

The Call Slip — search everything Ctrl·K / ⌘K