Table of Contents
Variables contain data that can change during the course of the program. A program like the ubiquitous “Hello World” program that we ran earlier, displays exactly the same information every time it runs, but this is rare. Most programs need to store some information while they are running. Variables are used for this purpose.
If you wanted to modify the “Hello World” program to display the name of the current user, you would need a variable to store the name.
Similarly, If you wanted to count the number of times a certain action was performed you would use a variable.
All variables are assigned a name so that we can keep track of them. This name must be unique within a programming unit. The programming unit can be anything from the whole program down to a do group. The requirement is simply that the name remains the same. For this purpose the case of letters is irrelevant and a variable called HELLO is the same as one called hello or Hello.
There are rules about naming variables:
- Variable names can include letters, numbers and the underscore character (_).
- Variable names can not begin with a number (1num is not a valid variable name while num1 is).
- Variable names can not include spaces.
- There are very few restrictions on variable names. It is even acceptable for variable names to be the same as keywords. It is always possible, however, that where the name is the same as a keyword, the interpreter may choose the wrong value. There are rules for determining which name to use, but sometimes it may be necessary to force LynPlex to choose the right one. This is explained later.
In the LynPlex interpreter variables may be implicitly declared, by simply using them. For example:
will automatically create a variable called var if the name has not been used before.
While this is acceptable when trying to get a small script running quickly, it has potential for causing great confusion in larger programs. In this case it is better to declare them first.
To declare a variable, use the DIM command, like so:<BR>
<BR> <CODE> DIM var<BR> </CODE> <BR> To declare multiple variables, seperate their names by commas:<BR> <BR> <CODE> DIM var1, var2, var3<BR> </CODE> <BR> Because there are different types of data you will have different types of variables and different ways to identify them. This is where the complexity of variables really gets started. But to put it simply, you have two main types of variables, those that store numerical values (51, 893.5, 0.005, etc.) and those that store text ("Hello World", "A", "Neil Obremski", etc.).<BR> <BR> Variables that store text are called strings, because they store a string of characters, and in memory they use a string of bytes to store each character. Generally strings take the most data (any string over 8 characters long will take more than any numerical variable you'll ever use) and cannot be used to solve mathematical equations. This makes sense of course, when have you ever heard multiply "Neil Obremski" times 5.<BR> <BR> Variables that store numbers I will refer to now only as numerical variables because there are many different types of numerical variables. Numerical values take very little memory because their values only require 2-8 bytes. Numerical values are used for mathematical equations. To use a variable of a specific type you can do one of two things: You can declare it (with DIM) as a specific type or you can add a symbol to the end of its name to signify its type.<BR> <BR>