Difference between revisions of "Global variables"

From Free Pascal wiki
Jump to navigationJump to search
m
(produce some content)
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 
{{Global variables}}
 
{{Global variables}}
  
A global [[Variable|variable]] is a variable that is declared in the main section of a [[Program|program]] or the [[Interface|interface]] part of a [[Unit|unit]]. Globals declared in a program cannot be accessed from within a unit. Globals declared in a unit can be accessed from within a program or another unit.
+
A [[Variable|variable]] is global if it is exported from a module.
 +
This usually refers to variables declared in a [[Var|<syntaxhighlight lang="pascal" inline>var</syntaxhighlight> section]]
 +
* prior any other nested [[Block|block]] in a [[Program|<syntaxhighlight lang="pascal" inline>program</syntaxhighlight>]], or
 +
* in the <syntaxhighlight lang="pascal" inline>interface</syntaxhighlight> part of a [[Unit|unit]].
  
<syntaxhighlight>
+
Global variables can be accessed from all other modules that import the exporting modules.
program GlobalVariables;
+
Note, however, that a <syntaxhighlight lang="pascal" inline>program</syntaxhighlight> can not be imported.
 +
 
 +
<syntaxhighlight lang="pascal">
 +
program globalVariableDemo(input, output, stdErr);
 
var
 
var
  g: integer;
+
x: integer;
 +
 
 +
procedure doMagic;
 +
begin
 +
// here, x is global to doMagic
 +
end;
 +
 
 +
procedure foo;
 +
var
 +
// shadow the global x
 +
x: integer;
 +
begin
 +
// here, x is local,
 +
// as the top-scope x can not be accessed
 +
end;
 +
 
 +
// MAIN //
 
begin
 
begin
 +
// here, x is local
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
  
'''g''' is a global variable.
+
== remarks ==
 +
If speed matters, global variables are/were used for frequently invoked routines, since allocating [[Local variables|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|<syntaxhighlight lang="delphi" inline>resourceString</syntaxhighlight> variable]] is always global.
  
== External variable ==
+
The [[FPC]] supports thread variables, too.
 +
They are sort of half-way between global and local variables.
 +
A [[ThreadVar|<syntaxhighlight lang="delphi" inline>threadVar</syntaxhighlight>iable]] is local to a thread.
  
External variable is a global variable has been declared in unit interface section.
+
== see also ==
 +
* [[Scope|Tutorial: Scope]]
 +
* [[Singleton Pattern|singleton pattern]]
 +
* [https://en.wikipedia.org/wiki/Global_variable Article “Global variable” on the English Wikipedia]
  
== Read More ==
+
[[Category:Code]]
* [[Var]]
 
* [[Local_variables]]
 

Revision as of 12:44, 16 October 2019

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);
var
	x: integer;

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

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

// MAIN //
begin
	// here, x is local
end.

remarks

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