Variables

A variable is a data structure. Variables can hold one piece of data of one data type.

Why use variables?

Variables are reusable. If a value changes, no problem!

Things like a character's name, health, and number of lives. Anytime you need you need to reserve computer memory to store a single number or single piece of text, you will store them in a variable.

Assigning / Storing a Value in a Variable

To store a value in you use the equals sign.

myName$ = "Bobo, the funny monkey"

Assign vs. Store

Some programmers use the term "assign a value to" rather than "store a value in" a variable. There is no difference except in the terminology used.

Whatever is on the right side of the equals sign will be stored in the variable on the left side.

Creating / Declaring / Defining Variables

Variable Names

Valid variable names can begin with a letter, number, or underscore ( _ ). Variable names cannot contains spaces or other non alphanumeric characters except the underscore.

Failing to properly name your variables with valid identifiers (names) will cause an error

  • Explicit - use explicit variable declaration
  • Destroy Variable
  • Draw | Display | Show Variable
  • Global Variables
  • If Variable
  • Local
  • Set Variable

Data Types

The "data" stored in variables usually has to be of a specific type. There are three kinds of data types in PlayBasic. String variables are used for holding messages and other things dealing with ''text''. Integer variables are used for math-related operations such as hit points. Real (floating point) variables are used for anything that requires a decimal fraction.

Related Pages

Reference

  • Dec - decrement a variable
  • Global - declare a global variable
  • Inc - increment a variable

How To's

Tutorials

Links

Categories: Data : Variables