A simple program without functions

Having an understanding of how our iPhone reads our code as a line by line recipe can help us understand the need for the concept of a function.

Let's imagine, for a moment, that we do not have any concept of functions when writing our code. Then, if we want to make a simple app that simulates the making of a pizza, we can write the following code:

print("Flattening the dough to form a round pizza A simple program without functions")
print("Adding some tomato sauce A simple program without functions")
print("Adding some mozzarella cheese A simple program without functions")
print("Adding some spicy pepperoni A simple program without functions")
print("Preparing the pizza in the oven A simple program without functions")
print("Done! Delicious pizza ready to be eaten A simple program without functions")

When we run this application, it will simply output the simulation of making a pizza to the console:

A simple program without functions

How will we write our code if we want our program to make multiple pizzas? The first thing that comes to mind is to repeat our code for each pizza that needs to be made:

print("Flattening the dough to form a round pizza A simple program without functions")
print("Adding some tomato sauce A simple program without functions")
print("Adding some mozzarella cheese A simple program without functions")
print("Adding some spicy pepperoni A simple program without functions")
print("Preparing the pizza in the oven A simple program without functions")
print("Done! Delicious pizza ready to be eaten A simple program without functions")

print("Flattening the dough to form a round pizza A simple program without functions")
print("Adding some tomato sauce A simple program without functions")
print("Adding some mozzarella cheese A simple program without functions")
print("Adding some spicy pepperoni A simple program without functions")
print("Preparing the pizza in the oven A simple program without functions")
print("Done! Delicious pizza ready to be eaten A simple program without functions")

print("Flattening the dough to form a round pizza A simple program without functions")
print("Adding some tomato sauce A simple program without functions")
print("Adding some mozzarella cheese A simple program without functions")
print("Adding some spicy pepperoni A simple program without functions")
print("Preparing the pizza in the oven A simple program without functions")
print("Done! Delicious pizza ready to be eaten A simple program without functions")

In the preceding program, we're simulating that we're making three pizzas. If we want the ingredients on our pizzas to be different, we can now change it for each pizza and our program would then be able to make different pizzas when we run it.

When we look at the preceding code, it becomes clear that something can be improved. You might have noted how repetitive our code is. Having to be repetitive takes up a lot of lines of code, which can make it more difficult to understand, specially if we imagine applications that have thousands of lines of source code. It also doesn't scale very well. Let's say, instead of making three pizzas, we want to make 25 or 100 pizzas; then it starts becoming messy and harder to grasp. If you are working with a friend on an application, improving the code, will be easier for others, and to understand your code. Otherwise, you will end up spending a lot of time explaining how the code works. Also, if you paused working on your application for a couple of months and then decided to work on it again, it quickly becomes difficult to remember where you left it. If you don't think about writing code that is easy to understand and maintain, then you will end up spending a lot of time and mental energy on trying to understand your code. In our pizza application, one can say that it becomes more difficult to understand how our program works with each repetition we make.

Consider the following line in our code:

print("Adding some spicy pepperoni A simple program without functions")

What if want to use the A simple program without functions emoji instead of A simple program without functions to help illustrate that our program is putting pepperoni on our pizza? With our current program, we will have to go through our code and change it in three places because we're currently making three pizzas with pepperoni. As you can imagine, this becomes more difficult when the number of lines in our code grows; this is an example of why programmers want to avoid repetition.