Arrays, Loops and Functions as Explained to a Five-Year Old
If I had to explain arrays, loops and functions to a 5-year old this is how it would go. I have a 5-year old daughter so I imagined I was speaking to her when I wrote this. Here goes nothing...no I got this right little dude?!:
Arrays, loops and functions are different things inside a computer's brain that help it to think, make decisions and do stuff.
Arrays are like beaded necklaces that you can create. You can pop beads off or pop beads on. You can make them all the same color or different colors. You can add beads that have numbers, letters and words on them. During the Halloween season you can even add beads that yell “BOOOOlean”!
Computers have these kinds of necklaces. Instead of calling them necklaces, programmers call them arrays. Programmers are people that can speak a computer’s language. Computer don’t understand English. They only understand 0’s and 1’s so programmers have to know a special computer language in order to talk to them.
Arrays are invisible and hidden inside a computer's brain. A string of beads put together makes a necklace. On a computer, instead of being called a bead, it is called an element. All the elements put together make up a magical invisible "array necklace" in a computer!
Programmers can use those invisible computer necklaces, aka arrays, to organize things on a computer and on your iPad to make things happen. (Your iPad is also a computer btw) Here is an example: When your finger touches Netflix on your iPad, you are making those secret, magic computer necklace arrays come to life so you can watch Netflix shows! How does Netflix know which shows you watched last? Well, Netflix puts all your favorite shows into a magical necklace array! When you swipe through all of your favorite/most recently watched shows...you are swiping on a magical, invisible array necklace!
Loops are something that happens over and over again. Like the sun coming up every day or you going to bed every night by 8:30pm(Well, almost everyday!). Computers love to work hard and do things over and over again. So, computers love loops!
People tell computers to do all kinds of loops. For example, someone could tell a computer to add 1 + 1 over and over again. She can tell the computer to do it two times, 5 times, one hundred times or even 1 million times! The computer loves it! It's super fun for a computer...like playing Simon Says. If I say, "Simon says, jump one time." That is not a loop. But if I say "Simon says jump two times" then that is a loop! It's a small loop but still a loop. "Simon says jump 100 times!" would be a big loop! "Now jump 1000 times!" Oops, you lost, I did not say "Simon says"! :)
The way that programmers talk to computers is by typing on a keyboard. Computers have powerful brains, but it takes a lot of words to talk to them sometimes. Computers love words so programmers have to type a lot!
A long time ago, programmers did not have functions so their fingers hurt a lot. To help the programmers' fingers, a powerful wizard named Rene Descartes used his magic wand to create the very first function. Obviously that is all BS but hey, most of us tell our children that Santa Clause is real. Functions made programmers’ lives so much easier. Instead of having to type lots of words to talk to a computer, they can type one magic function word and that magic function word can type one hundred words! Sometimes one million words! All those words tell the computer to do different things...like add up numbers, make decisions or even find a photo of 8 unicorns with 4 rainbows on the internet! I'll show you a real-life example of what a function is! But first, let's make some ice cubes! Let's make a list of everything we need to do:
- Get an ice tray.
- Fill the ice tray with water.
- Open the freezer door and put the ice tray in the freezer.
- Wait for ice to freeze.
- Open the freezer door and take the ice tray out.
- Twist the ice tray carefully to get some ice out.
- Put ice in a glass. That is a lot of things to do to get ice isn't it? Can you think of something that would make this easier? Yes! An ice machine! What are the steps of getting ice from an ice machine? Yes! Just one step:
- Push glass against the ice machine's dispensing paddle.
That's it! It's so much easier using an ice machine, isn't it? The ice machine is just like a computer function. A function does something that normally takes a long time and that you do a lot and does it really, really fast. Anytime you need computer "ice" a function is ready to spit some out for you.