MissingLink für Verweise auf noch nicht erstellte Abschnitte:
Begriffe, für die auf eine Definition verwiesen wird; Altes grün 33AA11
Variables and constants
Variables
Variables are known to anyone who had math lessons. Variables are just used to store any data (input, calculation results, etc.) in the memory. By using the variable name one can access the value directly.
While these variables were called x or y in math, programmers shouldn't use such names. They just really complicate the readability of the source code. This experience is known to everyone who once tried to rework a programm he did several month ago.
Therefore an important rule: Give variables self-explanatory names. Upper or lower cases aren't that important, but you have to follow the rules of the
Identifier.
Data types
You should consider, what kind of values you want to store, before using a variable. Variables are placeholders or "container" for a value which need space in the memory. The data type is structural design, how a variable is created.
The following are the most basic data types in X-Script:
| Type | Range of values | Example | |
| | | Declaration | Assignment |
| Integer (whole numbers) | -2147483648 bis 2147483647 | var number: integer; | number:=14; |
| Real (floating-point number) | 5.0 x 10324 .. 1.7 x 10308 | var number: real; | number:=3.4; |
| String (characterband) | arbitrary number of characters | var text: string; | text:='Hello World!'; |
| Char (character) | 1 character | var symbol: char; | symbol:='a'; |
| Boolean (logical value) | true, false | var right: boolean; | right:=true; |
These are standard types which can be used generally. Especially for numbers, there exist more types. If it is i.e. for sure that only whole numbers from 1 to 50 are needed, one can instead of the type integer choose the type byte, which can only contain numbers from 0 to 255. The same is true for real numbers.
Declaration
You can't just use a variable. Previously, you have to announce it to the compiler, so that it can perform the neccessary type checks while compiling. The announcement is called declaration. In front of a declaration the
reserved word 'var' is written. First comes the name of the variable and after a colon the type. More than one variable name of the same type can be written in one line, seperated by commata.
Example:
1 2 3 4
|
var
number1, number2, number3: integer;
result: real;
text, input: string; |
Depending on the scope of validity this is possible in the following code parts:
Global Variables
They are frowned upon by some programmers, but they are possible: globale variables. Their value is accessible in the whole unit and in all units which include this unit. The declaration is done at the beginning of the script - according to the <uses-clause>:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
program Script;
var
anumber: integer;
procedure StartMission;
begin
end;
begin
MissionName:='Test';
MissionType:=mzUser;
end. |
Global variables can be allocated with a starting value at their declaration:
This kind of assignment uses a simple equal sign, not := With local variables, this initialisation isn't possible.
Locale variables
The complement to global variables are local variables. Here, a variable is declared at the beginning of a procedure or function. It can only be used within this code section. If the programme leaves this procedure/funtiocn, the blocked memory will be cleared, that is, no values can be accessed or stored any more. Thats how a declaration of a local variable could look like:
1 2 3 4 5 6
|
procedure IDoSomething;
var
text: string;
begin
...
end; |
Assignment
The assignment of values to variables is done by the symbols := ("follows from") in X-Script. In contrary to math, constructs like these are therefore possible:
During the runtime, the system will calculate the sum of x and 1 and put the result back to x. The new x will be the old x plus 1. In short: x will be increased by one. For this case, there also exists an own procedure: inc(x).
Constants
Constants are in the end variables, which can only be read, but not overwritten in the programme. They are declared at the same position as variables, but with the key word 'const', an equality sign and without giving a data type.
1
|
const version = '1.23'; |