PYTHON CORE · LESSON 47

Two inputs to function

A few arguments · about 11 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

The function gets two values.

In brackets, after the function name, two arguments are listed by comma. When calling, transmit two values in the same order.

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

✦ Function returns 5.

STEP 2

Names help make sense

You can call the arguments clearer: for example, price and count. Then you can immediately see what computes the function.

def cost(price, count):
    return price * count
print(cost(4, 2))

✦ Result: 8.

IN-CLASS PRACTICE

Type the code yourself

Reprint the function with two inputs and its call.

def multiply(a, b):
    return a * b
print(multiply(2, 4))
Open the editor for class work
HOMEWORK

Solve it yourself

Create add with a and b arguments. Return their amount, then call add with 2 and 3 and output the answer.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework