In the last blog, you learned about the basic syntax of computer code, and how it is a set of instructions to follow, like a recipe. However, the big difference between computer code and a recipe is something called abstraction. When you are following a recipe, the recipe often won’t tell you the exact processes to take to achieve a task. For example, when the recipe says to set the oven temperature, it will not tell you the individual buttons to press or to take out anything in the oven to ensure that it is functional. The recipe assumes that you know these steps because most humans already know this basic skill.
Computers are a lot different. You cannot assume a computer knows how to do anything beforehand unless it is a built-in function. For example, if you want a real life robot to walk, you cannot just tell it to walk; there needs to be a way for it to learn which motors to turn, at which speeds, and in which direction. However, it is also in some ways like a human. If code is written efficiently, you do not have to tell the computer how to walk every time you want it to walk. You can just explain it once before the computer does anything, and it will remember those steps and do it correctly every time after that.
Computers remembering a set of steps is called a function. A function is a block of code that will run every time you call it, so you do not have to rewrite a block of code multiple times. Functions have many advantages over regular code blocks. For example, you can easily edit and debug a block of code without going through and changing every part of it. There are also input options or parameters put into the computer to change something about how the function runs (similar to telling a human to “run at five mph” or “set the oven to 425° F”). Furthermore, functions also help to understand code better, through a process called abstraction.
Abstraction is a level of breaking down simple tasks into the smaller movements required to complete the task. For example, a high level of abstraction would be “walk to school”. If you were to break it down, you would say “walk on third street for one mile, then turn onto eighth avenue for half a mile”. You can continue to break this down into lower and lower levels of abstraction. However, once you learn what the lower levels of abstraction are, you can use abstraction to make it simpler, so you don’t have to think about the small steps, which is very useful both in real life and also in code. When something has a “high abstraction”, it is very simple and easy to understand. When something has a “low abstraction”, it is broken down into smaller tasks that make up a bigger task. All functions are a form of abstraction, as you are taking a block of code and putting it under a simpler name, thus making it easier to understand.
Mastering these techniques is the key to making readable and efficient code. For example, look at this code above and where a function is defined. The function prints out two numbers, one of which is the input and another which is three less than the input. Under the function, two numbers are being inputted into the function: four and seven (x is equal to four, and four plus three is seven). The output is four, one, seven, four. This can save time when creating a program where there is a specific action being done multiple times.
Another example of functions is in a program for the game Wordle. In this program, you need to print a list of letters in a specific way, many times throughout the code. It is therefore much faster to make a function called print_list, which prints out each item through a list one after the other. The function goes through every line of the list printing out each item, then ends the function with an empty print ( ) statement which simply ends the line. This is much more efficient than running a copy of this code every time you need to print out a list.
Using functions in this way is much more efficient than any other option. Always remember to use functions and abstraction in your own code.