Functions

INPUT · Slides

Hand in more than one value

01 / 07

Separate them with commas

A function is not limited to one catching name. Separate them with commas and you can catch as many as you like.

You write the call the same way. The first value goes into the first name, the second into the second.

def add(a, b):    return a + bprint(add(2, 3))print(add(100, 250))

Result

5
350

02 / 07

The order carries meaning

This is the thing to watch. Values go in in the order you wrote them. Swap the order and you have asked for something else.

Below, divide(10, 2) is 10 / 2 and divide(2, 10) is 2 / 10. Same function, same values, wildly different answers.

def divide(a, b):    return a / bprint(divide(10, 2))print(divide(2, 10))

Result

5.0
0.2

03 / 07

Let the names carry the meaning

a and b tell you nothing about which is which. Give the catching names real words and mistakes about order all but disappear.

amount(unit_price, count) reads as price first, quantity second. A name does the work of a comment.

def amount(unit_price, count):    return unit_price * countprint(amount(150, 3))

Result

450

04 / 07

Numbers that do not match are an error

Two catching names means two values, always. Hand in one and a TypeError stops you.

The end of the error carries the name that is missing, so you can see at once which one you left out.

def add(a, b):    return a + bprint(add(2))

Result

TypeError: add() missing 1 required positional argument: 'b'

05 / 07

Three or more works the same

Three, four, it makes no difference — separate them with commas. More of them does not change how you write it.

Too many, though, and the caller can no longer keep the order in their head. Around three is a good ceiling for readable code.

def payment(unit_price, count, postage):    return unit_price * count + postageprint(payment(150, 2, 300))

Result

600

06 / 07

A list can go in alongside

What you hand in need not be a single number or piece of text. A whole list travels as one argument.

The example below takes a list of scores and a pass mark and counts how many made it. Handing the data and the condition in separately lets you reuse the same function with a different mark.

def how_many(scores, threshold):    number = 0    for score in scores:        if score >= threshold:            number += 1    return numberprint(how_many([80, 55, 90], 60))print(how_many([80, 55, 90], 85))

Result

2
1

07 / 07

A dictionary goes in as it is

Hand in a dictionary and one person's worth of data travels together. Name, score, address — however many fields, still one argument.

With that, handing in and handing back are both covered. A function is yours to use now.

The next group looks at how a variable made inside a function cannot be seen from outside (scope), how to name a value as you pass it, and what to use when nothing is passed.

def introduce(person, words):    return f"{words}, {person['name']}"print(introduce({"name": "Yui", "score": 80}, "hello"))

Result

hello, Yui