Identificar diferenças entre variáveis local e global
Quando você cria um procedimento, ele pode funcionar em muitas variáveis, inclusive variáveis globais e locais. As variáveis locais só podem ser acessadas no procedimento em que são definidas; portanto, o escopo é limitado ao procedimento atual em que você está trabalhando.
procedure MyFunction()
var
myInt: Integer;
begin
myInt := 5; // The variable myInt is only available in this function
end;
procedure MyFunction2()
begin
myInt := 6; // The variable myInt is not in the scope of MyFunction2
end;
As variáveis globais podem ser acessadas de qualquer lugar no objeto em que estão definidas. Elas não podem ser acessadas fora do objeto. Somente procedimentos globais podem ser acessados por outros objetos.
var
myInt2: Integer
procedure MyFunction()
var
myInt: Integer;
begin
myInt := 5; // The variable myInt is only available in this function
myInt2 := 10; // myInt2 is accessible everywhere in the object
end;
procedure MyFunction2()
begin
myInt := 6; // The variable myInt is not in the scope of MyFunction2
myInt2 := 11; // myInt2 is accessible everywhere in the object
end;