Python: Functions
A function is just a bit of code that you can execute easily again and again – functions can take inputs called parameters and do things with these (just treat them as variables).
To use a function just type the function’s name, and then inside some regular brackets type it’s parameters (if the function has no parameters, then use empty brackets).
For example the “abs” function. This takes a number and finds it’s value away from 0. For example:
Abs(-15)
As you may have expected, the above function outputs 15 (as -15 is 15 away from 0).
If a function has 2 parameters you separate them with a comma:
FunctionName(ParamOne, ParamTwo)
Note that variables can be used with function.
There are only a certain amount of functions built into Python.
For example the ceil function rounds up a number (remember that the ceiling is always up).
At the moment Python doesn’t know what “ceil” is – this is because we haven’t imported the module that tells Python what ceil is.
To use the stuff from a module you need to import it, we can do this like so:
import math
In this case we are importing the “math” module as this is the module that contains the “ceil” function. If we now try to use the ceil function (it takes one parameter) it will still not work correctly, we need to tell Python to look inside the “math” module – we can do this like so:
math.ceil(9.7)
The “math” module contains loads of useful math functions that you may need, there are also lots of other modules for different purposes.
Back to Python

