The Art of Decomposition — Python Functions
Up until this point, we have been writing code in a very serial manner. Programs have been sets of instructions that we write to reach a goal. With functions, we introduce new paradigms and ways of thinking. Our scope increases with the ability to reuse code and create tools for ourselves.
Decomposition at a Distance
A big idea in programming and general technology is the concept of abstraction. Abstraction is the process in which we focus less on the details and more on the big picture. A coffee machine is a perfect example. With a coffee machine, you only need to worry about adding water and grounds. You don’t conceptualize how the water is heated or how the pump circulates the water to the top of the machine. This is abstraction, hiding the details so you can focus on the result.
Creating a function is much like building a coffee machine. You assemble it once and write how all the little pieces fit together. When it comes time to use that function, all you do is call it and give it some pieces of data without thinking about how the information is processed.
I always teach students that half of the battle with programming is cognitive overhead. The less you are required to keep inside your head, the more you can build. Functions allow you to figure out a small part of your problem now in exchange for having to solve it later. Functions are a natural way to decompose your problem into components that you can reuse and refine to your liking.
Function Anatomy
I like to picture a function as a box with two slots. One slot takes in something, the other pushes something out.
In this box, You can put any value. Based on the value provided, a process happens and we get some kind of output.
To craft a function, we start with the def keyword. It tells Python that we are defining a function. From there, we specify the name of our function and a placeholder name for a variable you will use in the following code.
Let’s pretend we are consistently squaring a number and dividing it by two. We would create our function, and in the parenthesis, we would put an arbitrary variable name. When we use our function, whatever value we put there will replace num.
The result of the calculation is returned. We use the return keyword to return the value. Returning a value is often a point of confusion for someone just getting into functions.
Return can be a tricky thing to understand. It’s the piece of data that comes out of the box. If I enter eighty-seven into the square_split function and assign it to the variable number, the value of the number will be the value of what is returned in the function. The function does the work. The result is then returned and placed inside our variable.
Not all functions that you create need to have input or return values. It’s not at all uncommon to have a function with no input and have it return a value. Likewise, it’s not uncommon to have a function with an indefinite amount of inputs that don’t return a value.
Your use case will determine how you use your functions. They are an essential part of programming.
Closing
Functions take a while to get a solid grasp if you’re just learning or self teaching. Practice will propel you into understanding faster than any conceptual explanation will. Be sure to start simple. Create a function that does a calculation that you’re tired of typing or create a function that outputs the time in the format that you like. The possibilities are endless. Have fun exploring them.