Blitz2D Newbies: Understanding Variables
by Krylar

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:


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.


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:


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:


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):


Conclusion:

Hopefully this has given you a decent grasp on how to use variables in BlitzBasic.

Until next time...cya!

-Krylar


For a printable copy of this article, please click HERE.


This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.