Mega Code Archive

 
Categories / Delphi / Keywords
 

Var - starts the definition of a section of data variables

1 Var Name1 : Type; {...} 2 Var Name1 : Type = Expression1; {...} 3 Name (Var Var1 : type {;...}); Description The Var keyword is used to start a section of variable definitions. The section is terminated by the next keyword in a program. Within the section, one or more variables may be defined. These can be of any data type. 1.Variables with no initial value These are defined with a given type, such as string or Integer but with no assigned default initial value. 2.Variables with an initial value These are defined with a given type, such as string or Integer with an assigned initial value. Only global variables may be initialised. 3.Variable routine parameters When passing data to a routine (function or procedure), you can prefix the parameter definition with Var if the variable itself is to be updated by the routine. This allows a caller to pass data to a routine that will be enriched by the routine. Related commands Const Starts the definition of fixed data values Function Defines a subroutine that returns a value Out Identifies a routine parameter for output only Procedure Defines a subroutine that does not return a value Type Defines a new category of variable or process Example code : Defining local and parameter variables Var // Local variables apples, bananas : Integer; begin // Initialise our fruit counts apples := 1; bananas := 1; // Show their initial values ShowMessage('Apples = '+IntToStr(apples)); ShowMessage('Bananas = '+IntToStr(bananas)); ShowMessage(''); // Increment them in the UpdateCounts routine UpdateCounts(apples, bananas); // Show their new values - only Bananas will be updated ShowMessage('Apples = '+IntToStr(apples)); ShowMessage('Bananas = '+IntToStr(bananas)); end; // A procedure to increment the passed parameter values procedure TForm1.UpdateCounts(count1: Integer; Var count2: Integer); begin // Increment both passed parameter values Inc(count1); Inc(count2); end; Show full unit code Apples = 1 Bananas = 1 Apples = 1 Bananas = 2