Another way to think of functions is as mini-programs within a program.
If a program is a list of tasks for the computer, a function is a list of subtasks for one of those tasks.
In fact, a program is just the starting function that is using other functions.
This allows us to use other programs within our programs as functions.
Perhaps someone wrote a smart program called shortest_path that calculates the quickest path from point A to point B.
If we wanted, we could use shortest_path as a function in our new food delivery application to show the delivery driver's route.
Conditions
It's time to introduce the core tools for making logical decisions and repeating commands within programs.
In computers, there's no maybe. Every question is answered with a yes or a no¹. Every choice with a do or don't.
Imagine that you were writing an app to control a lightbulb. The core of your logic would be a binary decision. If the switch is on, the bulb shines light, otherwise it doesn't.
💡 In fact, the word if is the essential tool in most programming languages for writing conditional yes/no logic.
Loops
Loops allow a program to do a command any number of times.
Picture playing your favorite song on repeat. The command play is looped over every time the song is finished.
We only have to write the command play once and then use a loop to repeat it until a user presses stop.
💡 Loops let us repeat doing a command any number of times
Footnotes
[1] The language of computers is made up of two letters, the 0 and the 1.
Even though computers have such a simple alphabet, they can still do incredible things, like show you this text that you're reading right now.