Difference between revisions of "Var"

From Lazarus wiki
Jump to navigationJump to search
(increase quality)
(14 intermediate revisions by 9 users not shown)
Line 1: Line 1:
[[Keyword]] which is used to mark section, where [[Variable|variables]] and their data [[Type|type]]s are declared.
+
{{Var}}
  
<delphi>
+
The [[Keyword|keyword]] <syntaxhighlight lang="pascal" inline>var</syntaxhighlight> is used to:
  program/unit
+
* start section of [[Variable|variable]] declarations, and
  (..)
+
* declare a formal parameter as penetrating mutable.
  var:(a,b):real;
+
 
  (..)
+
== Variable declaration section ==
  begin
+
In a [[Block|block]] the word <syntaxhighlight lang="pascal" inline>var</syntaxhighlight> starts a section of one or more variable declarations.
  (..)
+
<syntaxhighlight lang="pascal" highlight="1">
  end.
+
var
</delphi>
+
age: integer;
+
</syntaxhighlight>
[[category:Pascal]]
+
 
 +
Variables bearing the same [[Type|data type]] can be grouped, by separating their respective [[Identifier|identifiers]] by a [[Comma|comma]]:
 +
<syntaxhighlight lang="pascal" highlight="2">
 +
var
 +
firstName, lastName, address: string;
 +
</syntaxhighlight>
 +
 
 +
== Variable parameter ==
 +
The word <syntaxhighlight lang="pascal" inline>var</syntaxhighlight> prior a formal parameter declaration indicates that this [[Variable parameter|parameter is variable]], that means assigning values to it will affect the named parameter at the call site.
 +
<syntaxhighlight lang="pascal" highlight="1">
 +
procedure censor(var xxx: longWord);
 +
begin
 +
if (xxx = $4655434B) or (xxx = $6675636B) then
 +
begin
 +
xxx := $2A2A2A2A;
 +
end;
 +
end;
 +
</syntaxhighlight>
 +
After invoking <syntaxhighlight lang="pascal" inline>procedure censor</syntaxhighlight> the value of the variable that was supplied at the call site (i. e. where the procedure was called) will (possibly) have changed, too.
 +
 
 +
== See also ==
 +
* [[Const|<syntaxhighlight lang="pascal" inline>const</syntaxhighlight>]]

Revision as of 23:00, 28 October 2020

Deutsch (de) English (en) español (es) suomi (fi) français (fr) русский (ru) 中文(中国大陆)‎ (zh_CN)

The keyword var is used to:

  • start section of variable declarations, and
  • declare a formal parameter as penetrating mutable.

Variable declaration section

In a block the word var starts a section of one or more variable declarations.

var
	age: integer;

Variables bearing the same data type can be grouped, by separating their respective identifiers by a comma:

var
	firstName, lastName, address: string;

Variable parameter

The word var prior a formal parameter declaration indicates that this parameter is variable, that means assigning values to it will affect the named parameter at the call site.

procedure censor(var xxx: longWord);
begin
	if (xxx = $4655434B) or (xxx = $6675636B) then
	begin
		xxx := $2A2A2A2A;
	end;
end;

After invoking procedure censor the value of the variable that was supplied at the call site (i. e. where the procedure was called) will (possibly) have changed, too.

See also