Difference between revisions of "Variable"

From Lazarus wiki
Jump to navigationJump to search
m (→‎manipulation: insert some links)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{Variable}}
 
{{Variable}}
  
A '''variable''' is an [[Identifier|identifier]] associated with a chunk of memory, that can be inspected and manipulated during [[runtime]].
+
A '''variable''' is an [[Identifier|identifier]] associated with a chunk of memory that can be inspected and manipulated during [[runtime|run-time]] in accordance with an associated [[Data type|data type]].
  
 
== declaration ==
 
== declaration ==
Variables are declared in a [[Var|<syntaxhighlight lang="pascal" enclose="none">var</syntaxhighlight> section]].
+
Variables are [[Declaration|declared]] in a [[Var|<syntaxhighlight lang="pascal" inline>var</syntaxhighlight> section]].
In [[Pascal]] every variable has a [[Data type|data type]] already known at compile-time (and let it be the [[Variant|data type <syntaxhighlight lang="pascal" enclose="none">variant</syntaxhighlight>]]).
+
In [[Pascal]] every variable has a data type already known at [[Compile time|compile-time]] (and let it be the [[Variant|data type <syntaxhighlight lang="pascal" inline>variant</syntaxhighlight>]]).
 
A variable is declared by a <math>(\text{identifier}, \text{data type identifier})</math> tuple, separated by a [[Colon|colon]]:
 
A variable is declared by a <math>(\text{identifier}, \text{data type identifier})</math> tuple, separated by a [[Colon|colon]]:
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
Line 11: Line 11:
 
foo: char;
 
foo: char;
 
</syntaxhighlight>
 
</syntaxhighlight>
According to the data type's space requirements, the appropriate amount of memory is reserved on the stack, as soon as the corresponding [[Scope|scope]] is entered.
+
According to the data type’s space requirements, the appropriate amount of memory is reserved on the stack, as soon as the corresponding [[Scope|scope]] is entered.
Depending on where the <syntaxhighlight lang="pascal" enclose="none">var</syntaxhighlight>-section is placed, you can speak of either [[Global variables|global]] or [[Local variables|local]] variables.
+
Depending on where the <syntaxhighlight lang="pascal" inline>var</syntaxhighlight>-section is placed, you can speak of either [[Global variables|global]] or [[Local variables|local]] variables.
  
 
== manipulation ==
 
== manipulation ==
Variables are manipulated by the [[Becomes|assignment operator <syntaxhighlight lang="pascal" enclose="none">:=</syntaxhighlight>]].
+
Variables are manipulated by the [[Becomes|assignment operator <syntaxhighlight lang="pascal" inline>:=</syntaxhighlight>]].
 
Furthermore a series of built-in procedures implicitly assign values to a variable:
 
Furthermore a series of built-in procedures implicitly assign values to a variable:
* input/output routines like, <syntaxhighlight lang="pascal" enclose="none">get</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">put</syntaxhighlight>, [[Read|<syntaxhighlight lang="pascal" enclose="none">read</syntaxhighlight>]] and <syntaxhighlight lang="pascal" enclose="none">readLn</syntaxhighlight>, <syntaxhighlight lang="pascal" enclose="none">assign</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">close</syntaxhighlight>
+
* input/output routines like, [[Get|<syntaxhighlight lang="pascal" inline>get</syntaxhighlight>]] and [[Put|<syntaxhighlight lang="pascal" inline>put</syntaxhighlight>]], [[Read|<syntaxhighlight lang="pascal" inline>read</syntaxhighlight>]] and <syntaxhighlight lang="pascal" inline>readLn</syntaxhighlight>, [[Assign|<syntaxhighlight lang="pascal" inline>assign</syntaxhighlight>]] and <syntaxhighlight lang="pascal" inline>close</syntaxhighlight>
* <syntaxhighlight lang="pascal" enclose="none">new</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">dispose</syntaxhighlight> when handling [[Pointer|pointers]] to [[Object|objects]]
+
* <syntaxhighlight lang="pascal" inline>new</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>dispose</syntaxhighlight> when handling [[Pointer|pointers]] to [[Object|objects]]
* <syntaxhighlight lang="pascal" enclose="none">setLength</syntaxhighlight> when handling [[Dynamic array|dynamic arrays]]
+
* <syntaxhighlight lang="pascal" inline>setLength</syntaxhighlight> when handling [[Dynamic array|dynamic arrays]]
  
 
== definition ==
 
== definition ==
A variable can be defined, that means declared and initialized, in one term by doing the following.
+
In [[Extended Pascal]] a variable can be defined, that means declared and initialized, in one term by doing the following.
 +
<syntaxhighlight lang="pascal">
 +
var
 +
x: integer value 42;
 +
</syntaxhighlight>
 +
The [[FPC]] as of version 3.2.0 does only support Borland Delphi’s <syntaxhighlight lang="delphi" inline>=</syntaxhighlight> notation:
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
var
 
var
 
x: integer = 42;
 
x: integer = 42;
 
</syntaxhighlight>
 
</syntaxhighlight>
Note, this syntax is not original Pascal.
+
The VAX Pascal notation using <syntaxhighlight lang="delphi" inline>:=</syntaxhighlight> is not supported at all.
In Pascal declarations and assignments are kept apart by design.
 
This syntax kind of breaks that principle.
 
  
 
== access ==
 
== access ==
A variable is accessed, that means the value at the referenced memory position is read, by simply specifying its identifier (wherever an expression is expected).
+
A variable is accessed, that means the value at the referenced memory position is read, by simply specifying its identifier (wherever an [[expression]] is expected).
  
Note, there are a couple data types which are in fact pointers, but are automatically de-referenced, including but not limited to [[Class|classes]], dynamic arrays and [[AnsiString|ANSI strings]].
+
Note, there are a couple data types which are in fact pointers, but are automatically de-referenced, including but not limited to [[Class|classes]], dynamic arrays and [[Ansistring|ANSI strings]].
With <syntaxhighlight lang="pascal" enclose="none">{$modeSwitch autoDeref on}</syntaxhighlight> (not recommended) also typed pointers are silently de-referenced without the [[^|<syntaxhighlight lang="pascal" enclose="none">^</syntaxhighlight> (hat symbol)]] being present.
+
With <syntaxhighlight lang="pascal" inline style="white-space: nowrap;">{$modeSwitch autoDeref+}</syntaxhighlight> (not recommended) also typed pointers are silently de-referenced without the [[^|<syntaxhighlight lang="pascal" inline>^</syntaxhighlight> (hat symbol)]] being present.
 
This means, you do not necessarily operate on the actual memory block the variable is genuinely associated with, but somewhere else.
 
This means, you do not necessarily operate on the actual memory block the variable is genuinely associated with, but somewhere else.
  
Usually the variable's memory chunk is interpreted according to its data type as it was declared as.
+
Usually the variable’s memory chunk is interpreted according to its data type as it was declared as.
With [[Typecast|typecasts]] the interpretation of a given variable's memory block can be altered (per expression).
+
With [[Typecast|typecasts]] the interpretation of a given variable’s memory block can be altered (per expression).
  
 
== memory alias ==
 
== memory alias ==
In conjunction with [[Keyword|keyword]] [[Absolute|<syntaxhighlight lang="pascal" enclose="none">absolute</syntaxhighlight>]] an identifier can be associated with a previously reserved blob of memory.
+
In conjunction with the [[Keyword|keyword]] [[Absolute|<syntaxhighlight lang="pascal" inline>absolute</syntaxhighlight>]] an identifier can be associated with a previously reserved blob of memory.
While a plain (identifier, data type) tuple actually sets a certain amount of memory aside, the following declaration of <syntaxhighlight lang="pascal" enclose="none">c</syntaxhighlight> does not occupy any additional space, but links the identifier <syntaxhighlight lang="pascal" enclose="none">c</syntaxhighlight> with the memory block that has been reserved for <syntaxhighlight lang="pascal" enclose="none">x</syntaxhighlight>:
+
While a plain <math>(\text{identifier}, \text{data type})</math> tuple actually sets a certain amount of memory aside, the following declaration of <syntaxhighlight lang="pascal" inline>c</syntaxhighlight> does not occupy any additional space, but links the identifier <syntaxhighlight lang="pascal" inline>c</syntaxhighlight> with the memory block that has been reserved for <syntaxhighlight lang="pascal" inline>x</syntaxhighlight>:
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
var
 
var
Line 49: Line 52:
 
c: char absolute x;
 
c: char absolute x;
 
</syntaxhighlight>
 
</syntaxhighlight>
Here, the memory alias was used as a, one of many, strategies to convince the [[Compiler|compiler]] to allow operations valid for the [[Char|<syntaxhighlight lang="pascal" enclose="none">char</syntaxhighlight> type]] while the underlying memory was originally reserved for a [[Byte|<syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight>]].
+
Here, the memory alias was used as a, one of many, strategies to convince the [[Compiler|compiler]] to allow operations valid for the [[Char|<syntaxhighlight lang="pascal" inline>char</syntaxhighlight> type]] while the underlying memory was originally reserved for a [[Byte|<syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>]].
 
This feature has to be chosen wisely.
 
This feature has to be chosen wisely.
It necessarily requires knowledge of data type's memory structure, if nothing is supposed to trigger any sort of access violations.
+
It necessarily requires knowledge of data type’s memory structure, if nothing is supposed to trigger any sort of access violations.
  
 
Most importantly, the additionally referenced memory will be treated as if it was declared regularly.
 
Most importantly, the additionally referenced memory will be treated as if it was declared regularly.
Line 58: Line 61:
 
== see also ==
 
== see also ==
 
* [[Constant|constant]]
 
* [[Constant|constant]]
 
[[Category:Pascal]]
 

Latest revision as of 18:16, 3 September 2022

English (en) suomi (fi) français (fr) русский (ru)

A variable is an identifier associated with a chunk of memory that can be inspected and manipulated during run-time in accordance with an associated data type.

declaration

Variables are declared in a var section. In Pascal every variable has a data type already known at compile-time (and let it be the data type variant). A variable is declared by a [math]\displaystyle{ (\text{identifier}, \text{data type identifier}) }[/math] tuple, separated by a colon:

var
	foo: char;

According to the data type’s space requirements, the appropriate amount of memory is reserved on the stack, as soon as the corresponding scope is entered. Depending on where the var-section is placed, you can speak of either global or local variables.

manipulation

Variables are manipulated by the assignment operator :=. Furthermore a series of built-in procedures implicitly assign values to a variable:

definition

In Extended Pascal a variable can be defined, that means declared and initialized, in one term by doing the following.

var
	x: integer value 42;

The FPC as of version 3.2.0 does only support Borland Delphi’s = notation:

var
	x: integer = 42;

The VAX Pascal notation using := is not supported at all.

access

A variable is accessed, that means the value at the referenced memory position is read, by simply specifying its identifier (wherever an expression is expected).

Note, there are a couple data types which are in fact pointers, but are automatically de-referenced, including but not limited to classes, dynamic arrays and ANSI strings. With {$modeSwitch autoDeref+} (not recommended) also typed pointers are silently de-referenced without the ^ (hat symbol) being present. This means, you do not necessarily operate on the actual memory block the variable is genuinely associated with, but somewhere else.

Usually the variable’s memory chunk is interpreted according to its data type as it was declared as. With typecasts the interpretation of a given variable’s memory block can be altered (per expression).

memory alias

In conjunction with the keyword absolute an identifier can be associated with a previously reserved blob of memory. While a plain [math]\displaystyle{ (\text{identifier}, \text{data type}) }[/math] tuple actually sets a certain amount of memory aside, the following declaration of c does not occupy any additional space, but links the identifier c with the memory block that has been reserved for x:

var
	x: byte;
	c: char absolute x;

Here, the memory alias was used as a, one of many, strategies to convince the compiler to allow operations valid for the char type while the underlying memory was originally reserved for a byte. This feature has to be chosen wisely. It necessarily requires knowledge of data type’s memory structure, if nothing is supposed to trigger any sort of access violations.

Most importantly, the additionally referenced memory will be treated as if it was declared regularly. No questions asked.

see also