What are local variables and global variables?

Local variables are declared in a procedure, global vaiables outside.

The terms Local and Global are, to my mind, out of date. Classifying variables as one or the other really relates to the way software used to be written, with a very simple structure. There are more levels now which don't have nice tidy names. To generalise, you need to consider what is called the scope of a variable, which means from where it can be accessed.

A traditional Local variable, as ddanbe says, was declared in a procedure (or function). In Pascal/Delphi it would be something like:

procedure DoSomething;
var
  MyLocalVariable : integer;
  AnotherLocal : string;
begin
  {Only code here could use these local variables}
end;

These are Local variables, and they can only be used within the DoSomething procedure. We say their scope is the DoSomething procedure. Any value stored in them is lost when DoSomething is complete. When that happens we say they they go out of scope, or are no longer in scope.

A traditional Global variable would be declared outside of a procedure like this:

program Project2;

var
  MyGlobal : integer;


function Func1 : integer;
var
  MyLocal2 : string;
begin
  {Code in Func1 can use MyLocal2 and MyGlobal}
end;


procedure Proc1;
var
  MyLocal1 : string;
begin
  {Code in Proc1 can use MyLocal1 and MyGlobal. It can call Func1.}
end;



begin
  { Code in main program can use MyGlobal, and call Proc1 and Func1 }
end.

Here MyGlobal is a Global variable. It is always in scope and it can be used from any code.

That has all applied since Adam was a small boy, and still applies today. It gets more complicated when you add units and classes into the mix. First consider a simple unit:

unit Unit1;

interface

var
  MyExternalUnitGlobal : Byte;

function SomethingGood : real;

implementation

var
  MyInternalUnitGlobal;

procedure DoSomethingBoring;
var
  MyLocalA : char;
begin
  {Code in DoSomethingBoring can access: MyExternalUnitGlobal
                                         MyInternalUnitGlobal
                                         MyLocalA}
end;

function SomethingGood : real;
var
  MyLocalB : word;
begin
  {Code in SomethingGood can access:
     MyExternalUnitGlobal
     MyInternalUnitGlobal
     MyLocalB
   Code in SomethingGood can call DoSomethingBoring}
end;

end.

Anything declared in the interface section can be utilised by any code in that unit, and by any code that uses that unit. The variables here (MyExternalUnitGlobal in this example) are not locals because they are not declared in a procedure or function. They are not globals because they are available everywhere - they are only available elsewhere if "elsewhere" includes this unit in its own uses clause.

Anything declared at the top of the implementation section can be accessed by any code in the implementation section. Sometimes called Unit Global. These variables can never be directly accessed from code that is not in this unit.

Then you have variables that are members of classes, which can have various scopes, but that is probably best left for another day.

Oh, and note that this idea of scope also applies to constants, procedures and functions.

commented: Nice explanation. +15
commented: Excellent discussion +15

since Adam was a small boy
Cute!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.