2.5. Functions#
Susanna Lange and Amanda R. Kube Jotte
Instead of writing a line of code multiple times to perform some action more than once, it might be advantageous to check if Python has a built-in function that already does what we want! If not, we can create our own functions in Python!
Both functions built-in to Python and those user-created follow the general format of mathematical functions of which you may be familiar. For example, the mathematical function f(x) = x + 2 takes an input, x, and returns x plus 2. Python functions work similarly. They require an input, called an argument, to the function, and they provide or compute some output. This section covers the foundations behind using built-in functions and how to create our own. Additionally, we consider methods in this section, which is a category of functions that pertain to a specific object, or type.