Variable Basics

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:


* Variable names can only be made up of Alphabet Letters (A through Z), Numbers (0123456789) and the underscore "_" character

* Upper and Lower case Alphabetical letters are considered that same. So the variable names "score", "Score" and "SCORE" would all represent the same variable.

* Variable names can start with a Alphabet Letter or the underscore character "_"

* Variables names can NOT start with a Number



  
; Legal Variable Names
  Score
  Players_Score
  Alien_Speed1
  
; Illegal Variables Names
  22_bullets ; Starts with a number - Illegal
  Player,Score ; Contains an illegal character ","
  




      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



Integer Variables only hold numeric values. These values can represent a numeric range of -2147483647 to 2147483647. Integer Variables can only contain whole numbers. (so no fractional values!). Integer variables can be identified easily as they no have a post-fix character eg. "Score"


Floating Point Variables hold decimal numbers (numbers with a decimal point). They are ideal for calculations that require fractional accuracy. Floating point variables can be identified by their "#" symbol post-fix. This classifies the variable to PlayBASIC as being of Float type. eg. "Gravity#"


String Variables hold a row text. String variables allow us to store and manipulate words/text and individual characters. These characters can be any character on your keyboard. String variables can be identified by their "$" symbol post-fix. This classifies the variable to PlayBASIC as being of String type. eg. "Name$"



     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.

Top






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 it'll see one Integer Variable called 'Score',
; This variable is being a assigned a value of 1000.
  Score = 1000
  


      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
  
; Here PB will see Two variables in the
; calculation "Score" and "Bonus"
  Score = Score + Bonus
  


      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)

  
; Here PB will see THREE variables.
; Those being "Score","Scre" and "bonus"
  Score = Scre + Bonus
  


      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.

Top







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.
  
; Tell PlayBASIC you wish to manually declare your variables
  ForceDeclaration
  
; Start a declaration list of variable names
  Declaration
     Score, Speed#, Name$
  EndDeclaration
  
; declare some variables using DIM
  Dim HighScore As Integer
  Dim PlayerSpeed As Float
  Dim PlayerName As String
  
  
  


      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.

Top







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

Top







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.

  
; (Assign the integer variable Score the integer
; value of 100 )
  Score =100
  
; (Assign the integer variable Lives the integer
; value of 3 )
  Lives =3
  
; (Assign the floating point variable Speed# the
; floating point value of 1.23 )
  Speed# =1.23
  
; (Assign the floating point variable Gravity# the
; floating point value of 1.23 )
  Gravity# =9.8
  
; (Assign the string variable PlayerName$ the string
; of "Silly Billy" )
  PlayerName$ ="Silly Billy"
  
; (Assign the string variable Language$ the string
; of "English" )
  Language$ ="English"
  



      The following examples demonstrate the basics of performing calculations between variables and storing the results.

Example #1,
  
; (Assign the integer variable Score the integer
; value of 100 )
  Score =100
  
; (Assign the integer variable 'HighScore' to equal
; whatever value the 'Score' variable contains.)
  HighScore = Score
  
  


      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,
  
; (Assign the integer variable Score the
; integer value of 100 )
  Score =100
  
; (Add 10 to the score variable)
  Score = Score +10
  


      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)
  
  
; (Assign the floating point variable Speed# the
; floating point value of 12.6 )
  Speed# = 12.6
  
; (Assign the Integer variable Time the integer
; value of 1000)
  Time = 1000
  
; (Mult the Speed# by Time and store the answer is a
; floating point variable Result#)
  Result# = Speed# * Time
  
  


      After this code runs the floating point variable "Result#" will equal 12600




Example #4, (String variables)
  
; Assign the String variable FirstName$ the
; string constant "Kevin"
  FirstName$ = "Kevin"
  
; Assign the String variable LastName$ the
; string constant "Picone"
  LastName$ = "Picone"
  
; Assign the string variable FullName$ the
; result of add the First and Last name
; variables together
  FullName$ = FirstName$ + " " + LastName$
  
; Display it
  Print FullName$
  
; Wait for a key to be pressed
  WaitKey
  
  


      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".

Top






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)

  
; Define a variable to hold the circles Xpos
; and make it equal to 200
  
  Xpos = 200
  
; Define a variable to hold the circles Y position
; and make it equal to 100
  
  Ypos = 100
  
; Define a variable to hold the circles radius
  Radius =20
  
  
; Use these variables as parameters to the Circle command
  Circle Xpos, Ypos, Radius, 0
  
  
; Add 10 to the Xpos variable
  Xpos = Xpos +10
  
; Draw another circle at the Xpos .
  Circle Xpos, Ypos, Radius, 0
  
; Wait for a key press
  WaitKey
  



      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


Top







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.

  
  A=10
  B=20
  C=40
  D = (A * B * C) +A-B
  


      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.

  
; Assign the String variable FirstName$ the
; string constant "Kevin"
  FirstName$ = "Kevin"
  
; Assign the String variable LastName$ the
; string constant "Picone"
  LastName$ = "Picone"
  
; Assign the string variable FullName$ the
; result of add the First and Last name variables together
  FullName$ = FirstName$ + " " + LastName$
  
; display the FullName$ string
  Print Fullname$
  
; Wait for a key press
  WaitKey
  


Top








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

Top






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

Top





 
Related Info: ArrayBasics | Comparisons | Dim | Explicit | Functions&Psub | Global | Literals | Local | Loops | NamingConventions | Operators | ProgramLayout | Types :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com