Functions are pieces of reusable code. Each function contains three descrete elements: name, input, and output. Functions take in input, called arguments or input arguments, and produce output. A function is called in Python by coding
output = function_name(input)
Note the output
is written first followed by the equals sign =
and then the function name and the input is enclosed with parenthesis ( )
.
Python has many useful built-in functions such as min()
,max()
,abs()
and sum()
. We can use these built-in functions without importing any modules.
For example:
In [3]:
out = sum([2, 3])
In the function above, the input is the list [2, 3]
. The output of the function is assigned to the out
variable and the function name is sum()
.
We can write our own functions in Python using the general form:
def function_name(input):
code to run indented
return output
A couple import points about the general form above. The def
keyword starts the function definition. Without the word def
you are writing a regular line of Python code. Afterthe function name, the input is encolsed with parenthesis ( )
followed by a colon :
. Don't forget the colon :
. Without the colon :
your function will not run. The code within the body of the function must be indentend. Finally, the key word return
needs to be at the end of you function followed by the output. The input
and output
variables can be any thing you want, but the def
, :
and return
must be used.
Let's create our own function to convet kilograms(kg) to grams(g). Let's call our function kg2g
.
The first thing to do is make sure that our function name kg2g
is not assigned to another function or keyword by Python. We can check if the name kg2g
has already been defined using Python's type
function. We know that sum()
is a function and def
is a keyword, how about kg2g
?
Let's first check if sum
is already a function name:
In [17]:
type(sum)
Out[17]:
Now let's check if def
is a keyword.
In [15]:
from keyword import iskeyword
iskeyword('def')
Out[15]:
OK, so how about kg2g
is that a function name?
In [18]:
type(g2kg)
g2kg
is not a function name. Now let's test to see if g2kg
is a keyword in Python:
In [1]:
from keyword import iskeyword
iskeyword('g2kg')
Out[1]:
Once we know that our function name is available, we can build our function. Remember the parenthsis, colon and return statment.
In [2]:
def kg2g(kg):
g = kg*1000
return g
Now let's try and use our function. How many kg's is 1300 grams. We expect the output to be 1.3 kg
In [3]:
kg2g(1.3)
Out[3]:
It is good practice to add a doc string to our function. A doc string is used to give the user an idea of what a function does. THe doc string is called when Python's help
function invoked. A typical doc string includes the following:
In [6]:
def kg2g(kg):
"""
Function kg2g converts between kg and g
input: a measurement in kilograms (kg), int or float
output: measurment in grams (g), float
Example:
>>> kg2g(1.3)
1300.0
"""
g = kg * 1000
return g
In [7]:
help(kg2g)
In [8]:
kg2g(1.3)
Out[8]:
In [ ]: