Difference between revisions of "Var"

From Lazarus wiki
Jump to navigationJump to search
(doc references are not working (anymore))
m (Fixed syntax highlighting; deleted category included in page template)
Line 1: Line 1:
 
{{Var}}
 
{{Var}}
 +
 +
 +
Back to [[Reserved words]].
 +
  
 
'''Var''' is a [[Keyword|keyword]] that is used for two different purposes:
 
'''Var''' is a [[Keyword|keyword]] that is used for two different purposes:
Line 8: Line 12:
 
Var is used to mark the section where [[Variable|variables]] and their data [[Type|type]]s are declared. Variables are typically declared at the beginning of a [[Program|program]], [[Procedure|procedure]], [[Function|function]] or [[Unit|unit]].
 
Var is used to mark the section where [[Variable|variables]] and their data [[Type|type]]s are declared. Variables are typically declared at the beginning of a [[Program|program]], [[Procedure|procedure]], [[Function|function]] or [[Unit|unit]].
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
var
 
var
 
   age: integer;
 
   age: integer;
Line 15: Line 19:
 
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]].
 
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]].
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
var
 
var
 
   FirstName, LastName, address: string;
 
   FirstName, LastName, address: string;
Line 23: Line 27:
 
When used before a parameter of a [[Procedure|procedure]] or [[Function|function]] '''var''' indicates that the parameter is a [[Variable parameter|variable parameter]]. A variable parameter can be used to receive data from, as well as send data to, a procedure or function:
 
When used before a parameter of a [[Procedure|procedure]] or [[Function|function]] '''var''' indicates that the parameter is a [[Variable parameter|variable parameter]]. A variable parameter can be used to receive data from, as well as send data to, a procedure or function:
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
procedure foo( var v1: sometype; out v2: sometype; const v3: sometype )
 
procedure foo( var v1: sometype; out v2: sometype; const v3: sometype )
 
begin
 
begin
Line 32: Line 36:
 
</syntaxhighlight>  
 
</syntaxhighlight>  
  
 +
== See also ==
  
== See also ==
 
 
* [[Variable parameter]]
 
* [[Variable parameter]]
 
* [[Local variables]]
 
* [[Local variables]]
 
* [[Global variables]]
 
* [[Global variables]]
 
 
 
[[category:Pascal]]
 
[[Category:Reserved words]]
 

Revision as of 07:17, 3 March 2020

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


Back to Reserved words.


Var is a keyword that is used for two different purposes:

  • signify the start of a variable definition section
  • make a function or procedure parameter be passed by reference instead of passed by value

Variable Definition

Var is used to mark the section where variables and their data types are declared. Variables are typically declared at the beginning of a program, procedure, function or unit.

var
  age: integer;

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.

var
  FirstName, LastName, address: string;

Pass by Reference

When used before a parameter of a procedure or function var indicates that the parameter is a variable parameter. A variable parameter can be used to receive data from, as well as send data to, a procedure or function:

procedure foo( var v1: sometype; out v2: sometype; const v3: sometype )
begin
  v1 := v1 + v3; // input and return value
  v2 := v3;      // only return value
  v3 := myconst; // immutable... only input
end;

See also