Introduction:
This article deals with the little containers known as "variables". If you've taken basic algebra, you already know what this is. If not, try this definition out:
A variable can be thought of as a container, much like Tupperware. Inside the container sits almost anything you want; counting numbers, characters, decimal (or floating-point) numbers, image data, etc. You give the container a name, preferably something that's understandable and clearly identifies the variables purpose, and then you put values in it.
What do variables look like in BB?
When you first see a variable, it can look a little confusing. It's actually very simple though. All you do is create a name that makes sense, tell BB what kind of variable it is, and assign a value to it.
For example, here's what a numeric variable looks like in code:
iNumber = 200 |
Now the variable iNumber holds the value 200. So instead of entering 200 throughout your program, you can simply use iNumber. This is important because you may need to change the value held in iNumber dynamically...something you'll have a really tough time doing without using a variable. We'll get into examples in a bit...for now, let me explain the different variable types used by BlitzBasic.
To best describe each, I'll show the source for them and then pick them apart.
iNumber iNumber% = 200 fNumber# = 200.125 sText$ = "Hello!" |
The first two, iNumber and iNumber%, do effectively the same thing. It's just that the default variable is considered an integer (or real number) so the % isn't necessary. Also, notice that there is no value assigned to the first iNumber. You don't have to assign a value when you declare the variable...it's usually best if you do, but it's not necessary.
The third line, fNumber# = 200.125, uses the # to tell BB that we want this variable to be of type floating point.
Finally, sText$ uses the $ to tell BB to treat this variable as a string. A string, sometimes called a "scalar", is simply a collection of characters.
Now you may be wondering at my choice of variable names. Since I like to be able to look at a variable and immediately know what it's for and what type of variable it is, I use a little naming convention. I put a lower-case "i" for integers, "f" for floating point(I'll also use a "d" for this sometimes because in C I use doubles, which is similar to floating point), and "s" for string (or character) variables. From there I add a descriptive name to the variable. Also, if the variable is more than one word, I capitalize each word. Here are some examples:
iNumberOfHitPoints = 125 iShieldValue = 100 iArmorValue = 50 fShipVelocity# = .500 fShipBrakingSpeed# = .250 sShipName$ = "Kliaziaan Warhawk" sPlayerName$ = "Krylar" |
Global and Local Variables
There are two types of variables in BB, global and local. A global variable is one that is available to the entire program. This means that any function within the program can not only read the contents of that variable, but can also change that value.
As you may have guessed, a local variable is one that is only available to the function in which it is defined. You don't have to put the word "local" in front of a variable inside of a function. If nothing is put in front of the variable, it's defaulted to being local.
Here's an example of a global variable called iSpacebarHits. This program simply increases a value each time the spacebar is pressed, and displays the new value to the user. Note that until you hit the spacebar, the screen will remain blank. Here's the code:
; Call the BB "Graphics" routine to initialize the graphics mode Graphics 800, 600 ; Set up our GLOBAL value Global iSpacebarHits = 0 ; keep doing this piece until the user hits the ESC key While Not KeyHit(1) ; if the user hits the Spacebar If KeyHit(57) Then ; clear the screen cls ; call the function that adds to the iSpacebarHits variable AddToSpacebarHits() ; print out the value of iSpacebarHits Text 400,300,"iSpacebarHits = "+iSpacebarHits,1,1 Endif Wend End ; this is a function for increasing the iSpacebarHits variable by one Function AddToSpacebarHits() iSpacebarHits = iSpacebarHits + 1 End Function |
This program only works because we declared the iSpacebarHits variable as global, if you change that declaration to local you won't see your output go past 0.
You may be wondering why you'd want to use local variables at all. The reason is that you will often want to do calculations within a function but don't want to have all that function's data known to the rest of the program.
For example, let's say that each time the spacebar is pressed we wanted to find a random number between 1 and 5 to add to the iSpacebarHits value. It wouldn't hurt to have this value as a global, but it wouldn't help either (unless you're using it for more than one function). So, if you are just going to use a variable in the function and it doesn't need to be available to the rest of the program, just make it local.
Another reason is that having too many global variables can make your code quite confusing to others who are working with it. Also, it can be very confusing to you in the future if you have to revisit it for some reason.
So, here's that source code again, but using that Random number idea with a local variable (again, you'll get a blank screen until you hit the spacebar):
; Call the BB "Graphics" routine to initialize the graphics mode Graphics 800, 600 ; Set up our GLOBAL value Global iSpacebarHits = 0 ; keep doing this piece until the user hits the ESC key While Not KeyHit(1) ; if the user hits the Spacebar If KeyHit(57) Then ; clear the screen Cls ; call the function that adds to the iSpacebarHits variable AddToSpacebarHits() ; print out the value of iSpacebarHits Text 400,300,"iSpacebarHits = "+iSpacebarHits,1,1 EndIf Wend End ; this is a function for increasing the iSpacebarHits variable by a random number Function AddToSpacebarHits() ; define our local variable Local iRandomNumber=0 ; call BB's Random number function, asking for a number between 1 and 5, ; and put that value into iRandomNumber iRandomNumber = Rand(1,5) ; Add iRandomNumber to the current value contained in iSpacebarHits iSpacebarHits = iSpacebarHits + iRandomNumber End Function |
Conclusion:
Hopefully this has given you a decent grasp on how to use variables in BlitzBasic.
Until next time...cya!