Difference between revisions of "Variable"

From Lazarus wiki
Jump to navigationJump to search
(rewrite(input))
Line 1: Line 1:
 
{{Variable}}
 
{{Variable}}
  
A '''variable''' is a symbol and an allocation of memory wich contains content ([[Integer|integer]] [[Char|char]] etc..) you can define. They may be in the scope of the whole program ([[Global variables|global variables]]) or a [[Procedure|procedure]], [[Function|function]] or method only ([[Local variables|local variables]]).
+
A '''variable''' is an identifier associated with a chunk of memory, that can be manipulated during run-time.
  
== How to assign values to variables ==
+
== declaration ==
 +
Variables are declared in a [[Var|<syntaxhighlight lang="pascal" enclose="none">var</syntaxhighlight> section]].
 +
In [[Pascal]] every variable has a [[Data type|data type]] already known at compile-time.
 +
A variable is declared by a (identifier, data type identifier) tuple, separated by a [[Colon|colon]]:
 +
<syntaxhighlight lang="pascal">
 +
var
 +
foo: char;
 +
</syntaxhighlight>
 +
According to the data type's space requirements, the appropriate amount of memory is reserved on the stack, once the [[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.
  
The  character pair [[Becomes| := (becomes)]] is called assignment operator: you assign the value of what is on the right side of the := character pair to whatever is on the left side of the := character pair.
+
== manipulation ==
 +
Variables are manipulated by the [[Becomes|assignment operator <syntaxhighlight lang="pascal" enclose="none">:=</syntaxhighlight>]].
 +
Furthermore a series of built-in procedures implicitly assign values to a variable:
 +
* IO 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>
 +
* <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" enclose="none">setLength</syntaxhighlight> when handling [[Dynamic array|dynamic arrays]]
 +
 
 +
== definition ==
 +
A variable can be defined, that means declared and initialized, in one term by doing the following.
 +
<syntaxhighlight lang="pascal">
 +
var
 +
x: integer = 42;
 +
</syntaxhighlight>
 +
Note, this syntax is not original Pascal.
 +
In Pascal declarations and assignments are kept apart by design.
 +
This syntax kind of breaks that principle.
 +
 
 +
== memory alias ==
 +
In conjunction with [[Keyword|keyword]] <syntaxhighlight lang="pascal" enclose="none">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>:
 +
<syntaxhighlight lang="pascal">
 +
var
 +
x: byte;
 +
c: char absolute x;
 +
</syntaxhighlight>
 +
Here, the memory alias was used as a, one of many, strategies to convince the 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>]].
 +
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 ==
 +
* [[Constant|constant]]
  
 
[[Category:Pascal]]
 
[[Category:Pascal]]

Revision as of 03:10, 1 December 2018

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

A variable is an identifier associated with a chunk of memory, that can be manipulated during run-time.

declaration

Variables are declared in a var section. In Pascal every variable has a data type already known at compile-time. A variable is declared by a (identifier, data type identifier) 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, once the 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

A variable can be defined, that means declared and initialized, in one term by doing the following.

var
	x: integer = 42;

Note, this syntax is not original Pascal. In Pascal declarations and assignments are kept apart by design. This syntax kind of breaks that principle.

memory alias

In conjunction with keyword absolute 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 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