Hey hey friends! Variables are one of the basic concepts you learn in programming. We use them all the time without thinking twice about them. So humor me and explain variables to me like I'm five.
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (10)
I can certainly take a crack at it.
Variables are words/labels that are used to represent information.
Think about what you ate for lunch. For example, let's pretend that you ate a hamburger for lunch. Now, you could tell me that, "I just ate lunch." I would then know that you ate lunch, but I have no idea what you ate. Of course, you would know that you ate a hamburger.
Let's fast forward a day, and you've yet again had lunch. This time you had a salad. You yet again tell me that "I ate lunch!" And yet again, I know that you're referring to some meal you ate in the middle of the day, but I don't know what you ate. Of course, you would know that you ate a salad.
You are able to use the word "lunch" to explain to me that you ate food at some time in the middle of the day. The meaning of "lunch" to me doesn't change. However, the meaning of the word "lunch", to you, changes everyday. That is, unless you eat the same thing several days in a row.
In this analogy, the word "lunch" represents the meal that you consumed on a specific afternoon. It can change at any time. That makes it a variable.
Variables make it easy to refer to information via some kind of label, such as "lunch". You could say to me that you had "lunch" on each day. Or, on the first day, you could tell me that you ate a hamburger around noon, and on the second day, you could tell me that you ate a salad around noon. Using a variable makes things much simpler, since you don't have to be explicit.
Awesome abstraction! I would say that if was a five year old, I would get this!
Mission accomplished! :)
This is a great question. Although Forest Hoffman nailed it, I'll still give it a shot.
Think of a variable as a pizza box. Now, the box may contain a pepperoni pizza, chicken pizza or no pizza at all. Just by looking at the box, you don't know what's in it. The only way to find out is to actually open the box.
Now, if I ask you to bring me the pizza from the refrigerator, you'll pick up the pizza box and hand it over to me. The box allows you to conveniently move the pizza from one location to another. It gives both of us a handy way to refer to the pizza and also move it around. So, the box is the variable and the pepperoni is the value of the variable.
That's a good way to explain it too. I think you could even extend this to talk about data types.
Thanks. That was the original intention. Wanted to also add the different types of boxes such as pizza box, cake box etc to show different data types. But then, I thought it would complicate the answer, so chickened out at the end. :D
Please do elaborate with the different boxes. Think it's helpful in visualizing how variables work. Plus you did an awesome job in illustrating with pizza. Pizza speaks dearly to my heart.
A variable is a box with a name and commitment problems.
Thanks Lorenzo for answering :) Was curious what you meant by commitment problems?
It was just a joke :) Since a variable can change value, the box is not committed to its content :)