Explaining Variables
Preparation
Write the following out on the whiteboard (it's best to have this done before the session). If a whiteboard isn't available, use a piece of paper or poster board so that it's clearly visible:
- How to sing "Old MacDonald":
- Old MacDonald had a farm
- E-I-E-I-O
- And on his farm he had a ___________
- E-I-E-I-O
- With a ___________ ___________ here
- And a ___________ ___________ there
- Here a ___________, there a ___________
- Everywhere a ___________ ___________
- Old MacDonald had a farm
- E-I-E-I-O
Activity
Talk about how hard it would be to write the song for a large number of animals
- Does anyone recognize the song on the board?
- This is actually a set of instructions for how to sing the song, but it's incomplete.
- What goes in the blanks?
- How many farm animals can you think of? (write a few on the board as they call them out)
- Now, what if we had to write these instructions out for every single farm animal? It would take a long time, right?
Next, try to get students to abstract the two placeholders needed:
- What should go in the blanks here?
- If we were to put a label next to each of these blanks to tell someone how to fill them in, how many _different_ labels do we need?
- The animal name goes first, and then we're just repeating the animal sound over and over.
- So we only have two labels: animal name and animal sound
Don't fill in the blanks just yet. For now, write the first variable in a space above the song.
- animalName = "Pig"
Next, explain what variables are in coding and how they can be used to solve our problem. Try to get the kids to give you the second variable and value.
- I'm going to write these out as variables . In coding, we call these labels "variables" because their value can change (or vary) from time to time.
- A variable is a named placeholder for a value. In this is example, animalName is the variable name, and Pig is the value.
- What's a name we could give to the other variable?
- What's should the value be in this case?
Write the other variable and value on the board.
- animalName = "Pig"
- animalSound = "Oink"
Now, ask students what to put in the blanks. Put animalName in the first blank, and animalSound in the other blanks. Try redefining the variables as something else (eg, a cow) and explain that now we only have to change the variables and not the song itself.
- Now that we have these labels, what do you think we can put in the blanks so that we don't have to change the whole song every time we want a new animal?
- We can use animalName in the first blank, and animalSound in all the other blanks.
- Now let's try changing the song for another animal. All we have to do is update the variables!
Summarize what we've learned:
- Now you know that variables can save us a lot of time. If you see yourself using the same value over and over again in the code, try using variables to save yourself some typing!
- The other benefit is that if we ever change our mind about the value, we only have to update it one place.