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.

- Constants, types and variables used in the
implementation
section are inaccessible to other units. This feature is not applied to the procedures and functions in the current version, but it will be added to the future ones. - Functions and procedures must have the same declarations in the interface and implementation section. Otherwise, compiler will report an error.
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
What do you think about this topic ? Send us feedback!