What Are Variables?
Developers use variables to save and store information that they want to access and reuse in the future. Learn how to create different kinds of variables and see how they can make your life easier as your code gets more complex.
Dealing with variables is one of the basic foundation blocks of programming. Introduction of variables into our programming solves many complications and helps us minimize code. The main role of the variable and its usage is discussed later when we start working with functions. For now, we just need to understand what a variable basically is and its main usage.
A variable holds data the way a pot holds spaghetti noodles. You could cook one at a time: we can't think of a reason why, but you could! Same goes for variables: they contain data instead of noodles. For instance, a variable (such as strData) is a perfect alternative to lengthy data such as a string. For example, if we want to print a string many times within the code, we may find it difficult to type it again and again especially if the data is comprised of many characters. In such cases, we can assign that data to any variable, use it in any part of the program and the compiler sees that variable as the real data that was initially assigned to it.
Hence, the code length can be reduced drastically with the use of variables. For example, take the string "Learning AS3 at 02geek.com is very easy." For some weird reason, we must use this multiple times in our program – say 100 times -- just to discover that we had a small typo in every single one (oh dear)... If we had one variable that contained this string we could have simply updated that variable and in so doing, updated each spot where that variable was used. So we should assign this data to a variable, say 'go02str': go02str now refers to the actual sentence wherever it is used in the program later.
The need for variables becomes mandatory when we use functions. Functions are declared first and can be called any number of times within a program: functions often have variables as arguments. For now we must remember that a variable can replace a lengthy string and can be used multiple times within the program. In the following videos we will be mainly working with variables, so don't worry if its not 100% clear yet: there's nothing like some real world practice!
The most important knowledge you should retain from this video is the understanding that variables are containers that contain data (such as our string) and can be used in place of that data, once they are defined.