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
triarea()
function with a couple of sets of input arguments.
In [2]:
triarea(10,5)
Out[2]:
In [3]:
A = triarea(1,4)
A
Out[3]:
triarea()
function, an error is returned:
In [ ]:
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'
The variables base
and height
are local variables. If base
or height
is called outside the function definition, an error is returned.
In [ ]:
triarea(base, height)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-4-1dd955b62482> in <module>()
----> 1 triarea(base, height)
NameError: name 'base' is not defined