8.4. Functions with Multiple Arguments

Functions can be written to accept multiple input arguments. When multiple arguments are specified, the arguments are listed within the parenthesis after the function name and separated by a comma:

def function_name(argument1, argument2):
    <code>
    return output

A function that calculates the area of a triangle given the base and height of the triangle would accept two arguments base and height. The formula for the area \(A\) of a triangle given base \(b\) and height \(h\) is below.

\[ A = \frac{1}{2} b \times h \]

Let’s name our function triarea and accept base and height as input arguments. The triarea function will return a number, the area of a triangle.

def triarea(base, height):
    area = 0.5 * base * height
    return area

We can test our triarea() function with a couple of sets of input arguments.

triarea(10,5)
25.0
A = triarea(1,4)
A
2.0

Note if only one input argument is supplied to the triarea() function, an error is returned:

triarea(2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_945727/3869252432.py in <module>
----> 1 triarea(2)

TypeError: triarea() missing 1 required positional argument: 'height'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-ddd55ccdd949> in <module>()
----> 1 triarea(2)

TypeError: triarea() missing 1 required positional argument: 'height'

The variables base and height are local variables. If base or height is called outside the function definition, an error is returned.

triarea(base, height)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/tmp/ipykernel_945727/3475441136.py in <module>
----> 1 triarea(base, height)

NameError: name 'base' is not defined
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-1dd955b62482> in <module>()
----> 1 triarea(base, height)

NameError: name 'base' is not defined