On a first look at functions in Python
Everybody needs to find a place in this world, and that includes your computer code. One of the best ways of making sure your code knows its proper place is by writing it in the form of a function. A function is kind of like a lego block. You write it in such a way that it can connect up nicely with other functions and lines of code. Together, they build something cool.
These structured blocks of code are used to perform an action and return some type of value. They’re modular, so you may be able to use them over and over. In fact, Python has lots of built-in functions, such as the “print” function, designed to make your life easier. You take its built-in blocks and snap them together with blocks of your own design.
It’s sort of like child’s play, but with more cursing.
You know a function when you see one because it starts with the keyword known as “def,” which stands for “define.” That is, you immediately define what your block of code does. It’s sort of like those nametags at business conferences: “Hi, I’m So-and-So, and Work at NameOfBiz as a NameOfOccupation.” Except functions somehow seem a lot less lame than nametags.
Function blocks start with def and then are followed by function’s name, parentheses and a colon (don’t forget the colon!). Like so:
But that’s not all, there’s more! A lot more, as you’ll see in the next post.
PS – If you’re the instant gratification type and want to learn more about functions right now, then here are some sites I recommend:
Featured image from GTurnbull925. Wikimedia Common. https://commons.wikimedia.org/wiki/File:Pile_of_lego_blocks.jpg