Variable: Difference between revisions

From BR Wiki
Jump to navigation Jump to search
(edit)
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{<[[String]] variable> | <[[numeric variable]]>}
A variable is a way of referring to a memory location used in a computer program.
A variable is a way of referring to a memory location used in a computer program.


Let us think that I ask you to retain the number 5 in your mental memory, and then I ask you to memorize also the number 2 at the same time. You have just stored two different values in your memory. Now, if I ask you to add 1 to the first number I said, you should be retaining the numbers 6 (that is 5+1) and 2 in your memory. Values that we could now for example subtract and obtain 4 as result.
There are two categories of '''variables''' in [[Business Rules!]].
 
#[[Numeric]]
#[[String]]
 
Additionally either of these types of variables may be contained in [[:Category:Arrays|arrays]], aka [[Mat]]rices.
 
==Example==
 
Let's imagine that I ask you to retain the number 5 in your mental memory, and then I ask you to memorize number 2 at the same time. You have just stored two different values in your memory. Now, if I ask you to add 1 to the first number I said, you should be retaining the numbers 6 (that is 5+1) and 2 in your memory. We could now, for example, subtract one from the other and obtain 4 as result.


The whole process that you have just done with your mental memory is a simile of what a computer can do with two variables. The same process can be expressed in BR with the following instruction set:  
The whole process that you have just done with your mental memory is a simile of what a computer can do with two variables. The same process can be expressed in BR with the following instruction set:  
Line 10: Line 21:
  00040 let result = a - b
  00040 let result = a - b


There are two categories of '''variables''' in [[Business Rules!]].
These are numeric variables. But the same can be done with characters and words, or [[string]] variables. For example, remembering the city "New York", and the landmark "Times Square". It can be stored as:
 
00050 let name$="New York"
00060 let landmark$="Times Square"
00070 print #255: landmark$&" is in "&name$


#[[Numeric]]
Notice that string variables must end with a dollar sign ($). Line 70 will return:
#[[String]]


Additionally either of these types of variables may be contained in [[Mat|arrays]].
Times Square is in New York.


==Naming Requirements==
==Naming Requirements==
Line 21: Line 35:
{{:Variable Naming Requirements}}
{{:Variable Naming Requirements}}


==Case Insensitivity==
==Variable Scope==


==Dimensioning Variables==
Variables in BR have global scope. This means that while your program is running, no matter what the current line it is executing, it knows the current value of all variables it contains. There is one [[Local variable|exception to this rule]], and it should be discussed after the reader learns the concept of [[User Defined Functions#Library Variables | User Defined Functions]].


==Variable Scope==
If you wish to pass the values of your variables to a different program after your program finishes executing, you may do so with a [[Chain]] statement.
 
==Initial Values of Variables==


==Initialization of Variables==
All string variables have an initial value of the empty string "". All numeric variables have an initial value of 0.


==Numeric==
==Numeric==

Latest revision as of 17:10, 29 June 2015

{<String variable> | <numeric variable>}

A variable is a way of referring to a memory location used in a computer program.

There are two categories of variables in Business Rules!.

  1. Numeric
  2. String

Additionally either of these types of variables may be contained in arrays, aka Matrices.

Example

Let's imagine that I ask you to retain the number 5 in your mental memory, and then I ask you to memorize number 2 at the same time. You have just stored two different values in your memory. Now, if I ask you to add 1 to the first number I said, you should be retaining the numbers 6 (that is 5+1) and 2 in your memory. We could now, for example, subtract one from the other and obtain 4 as result.

The whole process that you have just done with your mental memory is a simile of what a computer can do with two variables. The same process can be expressed in BR with the following instruction set:

00010 let a = 5
00020 let b = 2
00030 let a = a + 1
00040 let result = a - b

These are numeric variables. But the same can be done with characters and words, or string variables. For example, remembering the city "New York", and the landmark "Times Square". It can be stored as:

00050 let name$="New York"
00060 let landmark$="Times Square"
00070 print #255: landmark$&" is in "&name$

Notice that string variables must end with a dollar sign ($). Line 70 will return:

Times Square is in New York.

Naming Requirements

Variable Naming Requirements apply to both String and Numeric variables.

  • May contain any letter, number or underscore.
  • Must begin with a letter or an underscore.
  • May not contain dashes, spaces or any characters other than letters and underscores. In other words, characters such as - , ; : * ! @ # $ % ^ & ( ) + and many others are not allowed in a variable name.
  • May not exceed 30 characters.
  • May not start with "fn" as that prefix is reserved for User Defined Functions.
  • BR variable names are case-insensitive, so myVariable$ and Myvariable$ will refer to the exact same variable.
  • String variable names must end with a $.


Variable Scope

Variables in BR have global scope. This means that while your program is running, no matter what the current line it is executing, it knows the current value of all variables it contains. There is one exception to this rule, and it should be discussed after the reader learns the concept of User Defined Functions.

If you wish to pass the values of your variables to a different program after your program finishes executing, you may do so with a Chain statement.

Initial Values of Variables

All string variables have an initial value of the empty string "". All numeric variables have an initial value of 0.

Numeric

Numeric variables are one of two variable types available in BR.

Example

00010 let a = 10
00020 let b = a + 1
00030 let c = 2 * b

To learn more about the use of numeric variables, refer to the Numeric operators section


String

<1-30 alphanumeric characters and underscores, the first of which must be a letter (but not FN) which ends in a $. They also cannot be reserved words>

A string or string variable is a variable which may contain any type of characters. Strings are one of the two types of variables available in BR, the other is numeric. Strings may be formed into arrays of strings.

  • All normal Variable Naming Requirements apply
  • Variable name must end with $ (the $ does not count against the 30 character variable name limitation)
  • Default Dim Length of 18 characters

Below is an example of the use of a string in a short program:

00010 let myString$ = "BR kicks butt"
00020 print myString$

To learn more about the use of string variables, refer to the String Operations section.