Difference between revisions of "Var"

From Free Pascal wiki
Jump to navigationJump to search
(insert link to declaration)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{Var}}
 
{{Var}}
  
'''Var''' is a [[Keyword|keyword]] used to mark the section where [[Variable|variables]] and their data [[Type|type]]s are declared. When used for a parameter of a [[Procedure|procedure]] or [[Function|function]] then '''var''' indicates the use of a [[Variable parameter|variable parameter]].
+
The [[Keyword|keyword]] <syntaxhighlight lang="pascal" inline>var</syntaxhighlight> is used to:
+
* start section of [[Variable|variable]] declarations, and
Variables are typically declared at the beginning of a [[Program|program]], [[Procedure|procedure]], [[Function|function]] or [[Unit|unit]].
+
* declare a formal parameter as penetrating mutable.
  
<syntaxhighlight>
+
== Variable declaration section ==
 +
In a [[Block|block]] the word <syntaxhighlight lang="pascal" inline>var</syntaxhighlight> starts a section of one or more variable [[Declaration|declarations]].
 +
<syntaxhighlight lang="pascal" highlight="1">
 
var
 
var
  age: integer;
+
age: integer;
</syntaxhighlight>  
+
</syntaxhighlight>
  
If you are planning to use various variables of the same type, they can be grouped so they share the same type definition. The variables must be separated by a [[Comma|comma]].
+
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">
<syntaxhighlight>
 
 
var
 
var
  FirstName, LastName, address: string;
+
firstName, lastName, address: string;
</syntaxhighlight>  
+
</syntaxhighlight>
  
A '''var''' variable as a procedure or function parameter can bet used for a returnvalue:
+
== Variable parameter ==
<syntaxhighlight>
+
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.
procedure foo( var v1: sometype; out v2: sometype; const v3: sometype )
+
<syntaxhighlight lang="pascal" highlight="1">
 +
procedure censor(var xxx: longWord);
 
begin
 
begin
  v1 := v1 + v3; // input and return vlaue
+
if (xxx = $4655434B) or (xxx = $6675636B) then
  v2 := v3; // only return value
+
begin
  v3 := myconst; // immutable... only input
+
xxx := $2A2A2A2A;
 +
end;
 
end;
 
end;
</syntaxhighlight>  
+
</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 ==
 
== See also ==
* [[Local variables]]
+
* [[Const|<syntaxhighlight lang="pascal" inline>const</syntaxhighlight>]]
* [[Global variables]]
 
* [[doc:ref/refse19.html#x49-540004.2|Variable declaration]]
 
* [[doc:ref/refse21.html#x51-560004.4|Thread Variables]]
 
 
 
 
 
[[category:Pascal]]
 

Latest revision as of 22:14, 4 July 2021

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