Local and Global Variables


In Delphi, there are two types of variables: local and global. All previous examples in the book have used global variables. Global variables are variables that are declared outside of a function or a procedure. For instance, variables declared in the main Delphi project file or in the interface or implementation sections of a unit are global. Listing 5-8A shows several global variables.

Listing 5-8A: Global variables in the project file

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils,   Unit1 in 'Unit1.pas'; var   globalOne: string; { global variable }   globalInt: Integer = 100; { initialized global variable } begin   ReadLn; end.
image from book

Listing 5-8B: Global variables in a unit

image from book
 unit Unit1; interface var   s: string; { global variable that can be used in other units } implementation var   i: Integer; { global variable, but can only be used in this unit } end.
image from book

Global variables are allocated when the application starts, exist as long as the application is running, and are deallocated when the application ends. Not only are they available throughout the lifetime of the application, they are also publicly accessible. Every procedure and function that we create can access a public variable. One exception is when we declare a global variable in the implementation section of the unit. In that case, that variable is still global, but can only be accessed in the unit where it is declared.

Global variables are automatically initialized by the compiler to "empty". Integer values are set to 0, strings to '', and Boolean values to False. Global values can also be initialized manually at the same time they are declared:

var   x: Integer = 101;

Local variables are variables that are declared in a procedure or a function. Local variable declaration looks like this:

procedure ProcedureName; var   localVar_1: DataType;   localVar_n: DataType; begin end;

Local variables are pretty different from global variables. Local variables only exist for a short period of time. They are created when the procedure or function is called and are immediately destroyed when the procedure or function finishes. Local variables can only be used in the procedure or function where they are declared. Unlike global variables, local variables are not automatically initialized by the compiler and they can't be initialized at the same time they are declared. You should always manually initialize a local variable in the body of the procedure before you use it because, before initialization, local variables contain random values.

The for loop will actually issue a warning message if you use a global variable as the counter (see Figure 5-5). You should never use a global variable as the counter in a for loop because it diminishes the performance of the loop. When you use a local variable for the for loop counter, the compiler is able to use the CPU registers (the absolutely fastest memory locations on the computer) to perform the counting, which results in better performance.

image from book
Figure 5-5: The for loop doesn't like global variables



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net