Functions

INPUT · Slides

Show it, or hand it over

01 / 07

Two that look much the same

Compare the two functions below. The only difference is the one word, print or return. Both work out number * 2.

Call them, though, and only one line appears. give(5) shows nothing. That does not mean nothing happened.

def show(number):    print(number * 2)def give(number):    return number * 2show(5)give(5)

Result

10

02 / 07

print is for a human to look at

print is an instruction to put characters on the screen. The job is done the instant it happens, and no value is left anywhere.

Its business is reaching human eyes, so from the program's side nothing came back. Put the result of show(5) into a variable and what goes in is None.

def show(number):    print(number * 2)answer = show(5)print(answer)

Result

10
None

03 / 07

return is for the program to use

return is an instruction to hand a value back to where you called from. Handing over is all it does, so nothing appears on screen.

To see it, the receiving side passes it to print. Notice that the print in print(give(5)) below is outside the function.

def give(number):    return number * 2give(5)print("nothing has come out yet")print(give(5))

Result

nothing has come out yet
10

04 / 07

A printed value cannot go into a sum

Here is where people trip hardest. Try to use the result of a printing function in a sum and you get a TypeError.

10 is on the screen, and answer holds None. What your eyes can see and what the program is holding are two different things.

def show(number):    print(number * 2)answer = show(5)print(answer + 3)

Result

10
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

05 / 07

A returned value only shows if you say so

The trip goes the other way too. Simply call a function that returns and nothing appears on screen.

It is easy to conclude nothing is working, but the sum did finish and the value did come back. You just have to catch it and pass it to print to see it.

def give(number):    return number * 2give(5)print("the end")

Result

the end

06 / 07

You are allowed to do both

A function can of course print inside and return as well.

Below, the total goes on the screen while the amount goes back to the caller. Show a human and hand the program a value — when you want both, this is how.

def checkout(price):    print(f"that comes to ${price}")    return pricepayment = checkout(500)print(payment + 100)

Result

that comes to $500
600

07 / 07

Which one to write

The rule of thumb when you are unsure.

  • will the value be used later … if so, return
  • is it only there to be read … if so, print

A function you want to reuse as a part is better returning. Leave the showing to the caller and you keep your options open.

The with_tax below returns, so it can be shown on screen and added into a total. As a printing function, the total would have been out of reach.

Next we go past one argument and widen what a function can do.

def with_tax(price):    return price * 11 // 10print(with_tax(100))total = with_tax(100) + with_tax(200)print(total)

Result

110
330