Variables

From Liberty BASIC Family
Revision as of 14:25, 14 September 2020 by StPendl (talk | contribs) (Text replacement - "Building Blocks Categories" to "Building Blocks")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Not supported in Run BASIC

Description

A variable is a programming element which can have a value that is not fixed. The variable can be changed during the running of the program by assigning a new value to the variable name.
Variables can be of type string or numeric.

Syntax

  1. Numeric type:
    varName=numeric value
  2. String type:
    varName$="string value"

Hints

Naming Conventions

Generally, variable names should be chosen to give a hint as to what the contents may be.
A name must start with a letter, but it can contain letters and numbers.
You cannot use underscore ( _ ) in a variable name.
Variable names are case sensitive; name$ is not the same as NAME$ or Name$.
Having short variable names saves typing and lessens chance of misspelling; having descriptive variable names could make code more readable. Finding right balance is up to you.
The length of variable name in JB seems not restricted.
A standard naming practice is to use what is called "camel casing" (I suppose because the name looks "humpy"). This practice involves using multiple word names and capitalizing each word after the first. Like this:
myVariableName
A practice often used with other programming languages uses a dot to separate the descriptive words of a variable name. Like this:
my.firstName$
While in "other programming languages" dot usually represent nesting of objects, in JB it could be used for decoration only. Test shows that you can use several dots, several dots in a row and even end variable name with dot. (It is not common among other languages and will sure make your code less portable).

Numeric Variable

A numeric variable does not have a "$" appended to the name. It can hold a literal value, another variable value, or an expression. Some examples:
numVar = 3
numVar = var1
numVar = (numVar * var1 + 4)

String Variable

A string variable must have a "$" appended to its name. String variables can hold literal characters which must be enclosed in double quotes. They can also hold other string variable values. Some examples:
name$ = "Bob Smith"
number$ = "22"
firstName$ = word$(name$, 1)

Undefined variable

Variable, what does not assigned a value, is called "undefined". Undefined numeric variables return value 0 (zero); undefined string variable returns "" (empty string). Error in spelling variable name usually creates undefined variable.

Variable Scoping

In addition to using care with variable name spelling, you must also be aware of where a variable can be used in your program. In general, variables used in the main program will not be recognized in functions or subroutines; they are said to be not visible in subs and functions. An exception is when a variable is declared GLOBAL. For a discussion of scoping, see GLOBAL.

Example

firstName$ = "Bob"

print "Is your name "; firstName$; "?"

print "If your name is not "; firstName$; " then type your name after the prompt:"
input "My name is "; firstName$

print "OK. So your name is "; firstName$
print "Goodbye"

Useful Procedures

' Place a useful function or sub using this keyword here