Program Organization

Pascal imposes quite strict program organization. Below you can find models for writing legible and organized source files. For more information on file inclusion and scope, refer to Units and Scope and Visibility.

Organization of Main Unit

Basically, the main source file has two sections: declaration and program body. Declarations should be in their proper place in the code, organized in an orderly manner. Otherwise, the compiler may not be able to comprehend the program correctly.

When writing code, follow the model presented below. The main unit should look like this:

program { program name }
uses { include other units }

//********************************************************
//* Declarations (globals):
//********************************************************

{ constants declarations }
const ...

{ types declarations }
type ...

{ variables declarations }
var Name[, Name2...] : [^]type; [absolute 0x123;] [external;] [volatile;] [register;] [sfr;]

{ labels declarations }
label ...

{ procedures declarations }
procedure procedure_name(parameter_list);
  { local declarations }
  begin
    ...
  end;

{ functions declarations }
function function_name(parameter_list) : return_type;
  { local declarations }
  begin
    ...
  end;

//********************************************************
//* Program body:
//********************************************************

begin
  { write your code here }
end.

Organization of Other Units

Units other than main start with the keyword unit. Implementation section starts with the keyword implementation. Follow the model presented below:

unit { unit name }
uses { include other units }

//********************************************************
//* Interface (globals):
//********************************************************

{ constants declarations }
const ...

{ types declarations }
type ...

{ variables declarations }
var Name[, Name2...] : [^]type; [absolute 0x123;] [external;] [volatile;] [register;] [sfr;]

{ procedures prototypes }
procedure procedure_name([var] [const] ParamName : [^]type; [var] [const] ParamName2, ParamName3 : [^]type);

{ functions prototypes }
function function_name([var] [const] ParamName : [^]type; [var] [const] ParamName2, ParamName3 : [^]type) : [^]type;

//********************************************************
//* Implementation:
//********************************************************

implementation

{ constants declarations }
const ...

{ types declarations }
type ...

{ variables declarations }
var Name[, Name2...] : [^]type; [absolute 0x123;] [external;] [volatile;] [register;] [sfr;]

{ labels declarations }
label ...

{  procedures declarations }
procedure procedure_name([var] [const] ParamName : [^]type; [var] [const] ParamName2, ParamName3 : [^]type); [ilevel 0x123;] [overload;] [forward;]
  {  local declarations }
  begin
    ...
  end;

{ functions declarations }
function function_name([var] [const] ParamName : [^]type; [var] [const] ParamName2, ParamName3 : [^]type) : [^]type; [ilevel 0x123;] [overload;] [forward;]
  { local declarations }
  begin
    ...
  end;

end.
  Note :
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code