Global variables

From Lazarus wiki
Revision as of 12:44, 16 October 2019 by Kai Burghardt (talk | contribs) (produce some content)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

English (en) suomi (fi) русский (ru)

A variable is global if it is exported from a module. This usually refers to variables declared in a var section

Global variables can be accessed from all other modules that import the exporting modules. Note, however, that a program can not be imported.

program globalVariableDemo(input, output, stdErr);
	x: integer;

procedure doMagic;
	// here, x is global to doMagic

procedure foo;
	// shadow the global x
	x: integer;
	// here, x is local,
	// as the top-scope x can not be accessed

// MAIN //
	// here, x is local


If speed matters, global variables are/were used for frequently invoked routines, since allocating local variables on the stack takes time. This, however, is considered bad style. Nest your variables as deep as possible, but as high as necessary.

A resourceString variable is always global.

The FPC supports thread variables, too. They are sort of half-way between global and local variables. A threadVariable is local to a thread.

see also