A Course in FreeBasic: Chapter 2

Introduction:

Well, it's been some time since chapter one. And now we will continue on where I left off last time with Variables and Constants. In chapter one you got a little taste of these so you could see how the INPUT function worked. In this chapter, I will explain in pretty good detail the some of the many ways to use Variables and Constants, and a few of the codes that alter or render them.

Variables VS. Constants: In greater detail..

Variables are values in your program that will change over the course of its operation. Much like I said last time, things such as age, time, the position of something (That moves), or any number of things. Variables exsist in just about every program you run, and thus are a great way to control events in a game, or trigger errors when something goes wrong in your application, or game.

Constants are values in your program that do not change over the course of its operation. Things such as mathimatical constants (e.g., pi, g, etc.), a person's name, a creature's type (e.g. Horse, dog, cat, etc.) or many other things. Constants are very helpful when it comes to programs that use a lot of math constants so you only have to type the names insted of their long values.

Strings:

Strings are variables/constants that store text values, and numbers stored as strings are considered text. Stings are noted to the compiler by a $ symbol at the end of their name, unless they are DIMed as an string. Here is an example of the two types:

'A normal string..
stringname$ = "Text value.."

'A DIMed string (Show as DIM AS STRING, but can be DIM stringname AS STRING like in QBasic)..
DIM AS STRING stringname
stringname = "Text value.."

Strings have severial functions that can use or alter them. Two of them you already know as PRINT and INPUT. PRINT will read the value from the string and display it to the screen, where as INPUT stores a user defined value to the string. There are several commands that involve strings, but those will be covered in their correct time.

Numbers:

Numbers are variable/constants that may be of severial types. These are bytes, shorts, integers, longs, singles, and doubles. Each have their own perpose for storing numbers, and some can be either signed or un-signed. Here is a list of the data types:

BYTE: Is a 8-bit signed whole-number that can hold a
       value between: -128 to 127.
UBYTE: Is a 8-bit unsigned whole-number that can hold
        a value between: 0 to 255.
        
SHORT: Is a 16-bit signed whole-number that can hold
        a value between: -32,768 to 32,767.
USHORT: Is a 16-bit unsigned whole-number that can
          hold a value between: 0 to 65,365.
          
INTEGER: Is a 32-bit signed whole-number that can hold
           a value between: -2,147,483,648 to 2,147,483,647.
UINTEGER: Is a 32-bit unsigned whole-number that can
            hold a value between: 0 to 4,294,967,295
            
LONG: (Same as Integer)

LONGINT: Is a 64-bit signed whole-number that can hold
           a value between: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ULONGINT: Is a 64-bit unsigned whole-number that can hold
            a value between: 0 to 18,446,744,073,709,551,615
            
SINGLE: Is a 32-bit floating point number that can hold 
          a value between: 1.1 E-38 to 3.43 E+38.
          Its precision is of 6 decimal digits.
          
DOUBLE: Is a 64 bit floating point number that can hold
          a value between: -2.2E-308 to +1.7E+308.
          Its precision is of 14 decimal digits.

Data types are assigned to a variable/constant by the use of DIM. A quick note on how DIM can be used before we continue: DIM can assign the same type to multible values by using DIM AS (Type), or it can assign values to each one to be DIMed. You also have CONST which sets a constant to be caried over the whole program Examples:

DIM

'DIM all varibles AS BYTEs..
DIM AS BYTE variable1, variable2, variable3

'DIM each varible AS diferent types..
DIM variable1 AS BYTE, variable2 AS INTEGER 

CONST

'Constants
CONST PI = 3.14

CONST LENS = 256

Now that you see the basics on how DIM works, I will continue with an example code on using a few of the types so you can get an idea of their uses.. :

'DIM an Integer and a Single...
DIM numint AS INTEGER, numsng AS SINGLE 

'Give both a value of 100.5...
numint = 100.5

numsng = 100.5


'Print both values and see what you get...
PRINT numint; " <-- Integer."
PRINT numsng; " <-- Single."

SLEEP 

Basic ways to use Variables/Constants:

So, now that you know the different data types, you probaly want to have something to do with them. Here are some fun little sample programs that you can study and see a few ways to use these different types:

Variables

'Make a String and three Shorts
DIM AS STRING username
DIM AS SHORT syear, eyear, age

'Get user's name...
INPUT "Hello, who are you"; username
'Make a space so lines don't run together...
PRINT 
'Welcome user...
PRINT "Hi, "; username; "."
PRINT 
'Ask user for his/her birth year...
INPUT "What year were you born? (####): ", syear
PRINT

'Ask user for the current year...
INPUT "What is the current year? (####): ", eyear

'Calculate user's age..
age = eyear - syear

PRINT

'Tell the user how old the user will be on his birthday...
PRINT "On your birthday in"; eyear; ", you will be"; age; "."
PRINT

'Tell the user good bye...
PRINT "Have a nice day, "; username; "."

'Pause long enough for user to see the last message...
SLEEP 

Constants

'DIM are variables...
DIM AS SINGLE ANG ,RAD

CONST PI = 3.14 'Set the constant..

ANG = 40 'Our angle to make a Radian..

RAD = ANG * PI / 180 'Make the angle a Radian..

PRINT "Original Angle:"; ANG
PRINT

PRINT "The Radian value:"; RAD
SLEEP 

The comments explain what the code does, and you should by now be able to understand the commands I used. Well, that wraps up this tutorial on using variables, so until next time, happy coding, and have fun!

PS: No debugging tips since the tips from the last chapter will work here if problems arise.

Contacting me:

EMail: Rattrapmax6 on AOL
Webpage: My Site...