In Delphi the scope of a variable is determined by where it is declared. For example a variable declared within a function or procedure is 'in scope' (or known and visible) only within that particular function or procedure. The variable is created when the function or procedure is entered and executed and then once the function/procedure exits, the variable goes 'out of scope' and is also destroyed and the memory it consumed is free. A variable declared within one procedure is not visible to any other procedure. A variable declared at the head of unit is considered a global variable and is visible to every procedure and function within that unit. Note that because of caching of memory in high speed memory areas on the CPU it has been suggested that global variables be avoided as much as possible since they can apparently cause a large number of cache miss penalties which slow down the performance of system operating with such a cache. (The cache is normally loaded with memory which is in the area of currently executing code, although this explanation is a bit of simplification, however a global variable might exist in a far segment of memory not currently in the cache, thus causing the cache miss).In Delphi variables are declared before they are used at the begiining of the function or procedure definition using the var statement, or within the declaration header of the procedure or function if it is a variable that is passed to the function by another segment of code..
function TForm1.ThisFunction(i : integer):integer;
var x : integer;
s : string;
begin
the code is here
end;
In this example the variable 'i' is passed to the function and thus is declared in the header. After the var statement the variables x and s are declared and then the body of the function begins after the begin statement.
In C the situation is more complex. Variables can be declared within the body of functions. This also introduces complications in regards to the scope of variables. For example we could have two variables named 'x' declared within the body of one C function.
int x=10;
for (int x=20;x<30;x++)
{
code of loop
}
y = x;
In this example there are two variables named x. The second variable, declared in the header of the for loop is in scope only so long as the for loop is executing. When the for loop is executing a reference to x refers to the x declared within the for loop since it is currently in scope. When the for loop exits and we set y equal to x, y gets the value 10 because the variable x declared outside the for loop is now the variable in scope. Code like this will not generate a compiler error since each variable named x has its own scope (an error will be generated however if two variables named x with the same scope are declared). One could imagine loops nested within loops within functions all of which declare variables named x, each one with a different scope.
Because variables are in scope only within the code segment that declares them statements such as the following will generate compiler errors.
int y;
for (int x=20;x<30;x++)
{
code of loop
}
y = x;
This generates a compiler error since x is only in scope within the loop where it was declared, and no longer exists outside the loop. The previous example just above however would be legal, and y would become 10 since this x has the same scope as y (it is visible to y).
Global variables in C which are visible everywhere must be declared, similar to Delphi, outside the main body of the program. This would look something like this...
#include <conio.h>
#pragma hdrstop
int x = 1;
program functions etc follow here
This variable x is global in scope, however if another variable named x is declared in a function or loop or other block of code then it has higher priority than the global variable and any reference to x would then refer to this local variable. The global variable x would only come into scope once again after the local variable x went out of scope.
Global variables are only global to the unit (source file) in which they are declared, however using the extern statement any global variable in any source file can become truly global and visible to other program modules. For example if we declared a global variable 'x' as in the example above, and then in another file we include the statement
extern int x;
the compiler recognizes that this unit will be using the global variable 'x' which is declared in one of the units 'included' at the head of the file. (Note that the ' #include ' statement in C performs the same functions as the ' uses ' statement in Delphi).
A Unified Field Theory
![]()
The Unified Field Theory
is also available as a zip file -> unified.zip
Introduction :The Pioneer Effect and the New Physics. A brief description of the new physics required to explain the 'Pioneer Effect', which is the constant deceleration of space craft as they fly through space.

