A function is a way to create reusable pieces of code that can have variables inputted into.
Python has hundreds of functions built-in, a couple example include print()
and sort()
.
You can also write your own functions in Python to add your own reusable operations.
Let's define our own function in Python.
First, think of a easy name to remember and one that won't conflict with built-in functions.
Next, you'll want to type in def
and then the function name along with a set of parenthesis and a colon that end.
Finally create a new indented line and start writing code!
In Python, you can also create functions that take variables as an input. You can use this to write a long piece of code once and use it anywhere easily.
Here's an example of a very basic logging function, which takes a message and a severity number: