Skip to content

Functions with Multiple Arguments

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.

In [1]:
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.
In [2]:
triarea(10,5)

Out[2]:
25.0

In [3]:
A = triarea(1,4)
A

Out[3]:
2.0

Note that if only one input argument is supplied to the triarea() function, an error is returned:
In [4]:
triarea(2)

------------------------------------------------------------------------
TypeError                              Traceback (most recent call last)
<ipython-input-4-ddd55ccdd949> in <module>
----> 1 triarea(2)

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