|
By: Kevin Picone I N D E X: What are they? Variables are the foundation of every programming language. They're used to hold single pieces of information. This information can be changed, used in calculations and even displayed among other things. Each Variable is given a name by the user. It's best to make your variable names as readable and meaningful as possible. For example, to hold the players current Score, you might use a variable called "Score". This is easily distinguished and remembered as you work upon your program. But we could have also called it "Silly_Variable_Name5", "S" or "Zxasq123". While these would work equally. They're just not very readable or memorable, which often cause problems when developing your programs over time. So keeping your variables meaningful/readable, makes programming much easier ! While you can name your variables pretty much anything in PlayBASIC, there are some rules that dictate the style and what characters (letters) that can actually be used in variable names. Variable Naming Rules:
Coming up with a variable name is the first part of the hurdle, the second is deciding upon what type of variable it should be. This is very important, as it dictates the type of information this variable can hold. PlayBASIC gives us three basic variable types. Which are, Integer, Float and String
Throughout each program, you'll be using variables constantly. They'll be used in calculations and to hold information about the different aspects of your games. So it's very important you get familiar with the different types and the conventions that programming languages like PlayBASIC use. How do I Declare (create) variables ? By default, PlayBASIC (like most basic languages) allows variables to be assigned or used anywhere within your program without having to explicitly declare them. This makes programming quick and easy. However, you can force PlayBASIC to require Variables to be declared prior to use, by using the "Explicit" command. How does it automatically see my variables ? Automatic variable declaration occurs the first time PlayBASIC sees a new variable name in your program. PlayBASIC will look at the variable name and any following symbols to help it classify the type of variable it should create. It does this to make sure it's creating a variable that can hold the type of information you've specified. It's important to note, that if you Mistype a variable name, PlayBASIC will simply assume this mistyped variable is actually a new variable. Since there's no way for it to determine that you made a typo. Example #1
Here PlayBASIC will see the Integer Variable named "Score". Since this is the First time "Score" has been used in this program, the "Score" variable will be added to PlayBASIC internal list of variables. So the next time you use the Score variable name, you'll be referring to the same variable. Example #2
In this example, PlayBASIC will see two Integer variables. First, it'll see the "Score" variable at the start of the line, then it will notice the "Bonus" variable at the end of the line. Example #3 (Example Typo)
In this example PlayBASIC will see three integer variables in this line of code. First, it'll see the "Score" variable at the start of the line, then it'll see the mistyped "Scre" as new variable as well as the "Bonus" variable at the end of the line. While PlayBASIC will happily compile and run this code, if you mistyped "score" and entered "Scre" like in this example, this line of code wouldn't function correctly in your program. So some care is needed while you enter your program as Mistyping a variable name is one of the most common forms of programming mistakes. Which is why PlayBASIC oncludes an optional Explicit mode. Can I Manually Declare My Variables ? Yes, if you wish to override PlayBASIC automatic variable declarations and manually declare variables. Then you can use the Explicit or ForceDeclaration directive at the top of your program. This will make PlayBASIC force you to predeclare variables before they're used. To do this your variables need to either be listed between the Declaration / EndDeclaration markers, or declared using DIM, GLOBAL, LOCAL. I.e.
There is also Global, Local, Static variable declarations. But those are beyond the scope of this tutorial. They're covered in the Functions&Psub tutorial though. What Do Variables Contain Initially ? Nothing, when a variable is first created, it's contents are automatically cleared. So numeric Integers and Floating point variables will automatically be set to zero. Strings variables will default to holding an empty string. An empty string an be represented as a pair of talking marks with nothing between them. Ie "" would be an empty string How can I Store some information in a Variable ? Programming languages like PlayBASIC use a 'Result = Calculation' convention. Under this convention, we're assigning the RESULT side of the equals sign to the answer from CALCULATION side. It's important to note that the calculation side of the equation is evaluated first ! Also, when information is stored in a variable, what ever information the variable had previously contained is overwritten. The following example shows Integer, Float and String variables being assigned some information directly.
The following examples demonstrate the basics of performing calculations between variables and storing the results. Example #1,
In this example were first assigning the Score variable, then were assigning the HighScore variable to equal score. So when this code runs, Score will equal 100, and so will HighScore. Example #2,
In this example we're first assigning the Score variable a integer value of 100. Then in the second line of code, we're making the variable Score equal to the result of the addition calculation between the Score variable and the integer literal value of 10. Now since Score currently equals 100, our addition calculation is actually adding 100+10 together. So the result (110) will then be stored back into the Score variable. So after this code runs, the Score variable will now equal 110. Example #3, (floating point variables)
After this code runs the floating point variable "Result#" will equal 12600 Example #4, (String variables)
This example deals with string addition. Unlike mathematical addition, when you add string variables or string constants together, the addition operation joins the two strings together, creating a new string. If you look closely you'll notice the FullName$ calculation is adding three string parts together. It's adding the String Variable (FirstName$) + String Constant " " + String Variable (LastName$) . So upon execution the String Variable FullName$ will equal the string "Kevin Picone". Retrieving information stored in Variable ? When using variables we're either assigning them information or retrieving their information for use in a calculations. Basic Programming languages like PlayBASIC use a 'Result = Calculation' convention. Under this convention, we're assigning the RESULT side of the equals sign, to the answer of the CALCULATION side. It's important to note that the calculation side of this equation is evaluated first ! So we can retrieve the information's from a variable or an equation using variables and functions, by using our variable on the Calculation side of the expression. Which is what some of the previous examples have already shown. Also, we can pass variables into PlayBASIC commands. Which is one of their most common uses. Example (drawing a circle)
This example will draw two circles. Then First will be located at position 200x,100y on the screen and the second will be drawn at 210x,100y on the screen. See: Circle, WaitKey What Operations can be performed Between Variables ? The numerical variable types like Integer + Floating Point, can be mixed together with all the regular math operations. Like Addition (+), Subtraction(-) , Division(/) and Multiplication (*). An example of a simple equation between integer variables.
PlayBASIC also has a whole bunch of math functions built in, like COS() SIN() etc etc. Be certain to check the Maths section of the documentation for a full list of the available functions. String variables can Not be used in conjunction with Integer and Float variables, as they are not numeric. Since strings are used to store sequences of alphabetical characters, which could be anything. It's not impossible to treat them as numeric for calculations though, as there are various commands built into PlayBASIC that can translate strings into numbers and back again. Namely the VAL() and STR$() functions. PlayBASIC includes many built in functions for manipulating the information within string variables. So make sure you check the Strings documentation.
How many pieces of information can a variable store? One, while variables can be changed at any time. They can only hold a single piece of information at any one time. So If you need to store and retrieve many pieces of the information, then you should look into how to use an Array (See: ArrayBasics). What's Next ? If you're feeling up to speed on variables then you might like to read up on Literals, Arrays or perhaps even Types |
Related Info: | ArrayBasics | Comparisons | Dim | Explicit | Functions&Psub | Global | Literals | Local | Loops | NamingConventions | Operators | ProgramLayout | Types : |
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |