It all depends on visibility.
Types, variables, constants, procedures, and functions declared in the interface section of a unit (but outside of classes and other type definitions) are visible to other units, whereas types, variables, constants, procedures, and functions declared in the implementation section of a unit can only be used in the very same unit (and only below the declaration). Hence, if you need types/variables/functions/... in a particular unit but do not expect the identifiers to make sense outside the unit, then it is a good idea to declare them right before they are needed, in the implementation section.
Further, when it comes to classes, their identifiers can be declared as private, strict private, public, protected, and published. This is again due to different kinds of visibility. Private identifiers can only be used inside the class itself (or other classes defined in the same unit, unless strict private), and so on.
Also, notice this:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
alpha: integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
var
beta: integer;
implementation
{$R *.dfm}
end.
Since alpha
is a member of the class TForm1
, every instance of this class, that is, every object of this form (that is, every form created of this class) will have its own alpha
variable. On the other hand, beta
, being declared in the unit outside of any class, is "one per unit", that is, every TForm1
object will see the same beta
. (And then there are "class variables" and such. Please see the documentation for more details.)
(Also, you probably already know this, but in a case like
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm3 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.FormCreate(Sender: TObject);
begin
beep;
end;
end.
you do not have two functions named FormCreate
, but only one. The first reference to this function is the declaration, which is part of the class declaration in the interface
section, which is what other classes and units will see. The actual implementation of the FormCreate
function (or its definition) is always in the implementation
section. Indeed, other classes or units do not need to know the exact implementation of the functions in a particular class.)
Finally, I would like to recommend the official Delphi documentation, which is very good. Start at http://docwiki.embarcadero.com/RADStudio/en/Delphi_Language_Guide_Index.