Lazarus Pascal - An Introduction
Lazarus is an implementation of EVENT DRIVEN Pascal for windows. It
offers the programmer a rich collection of conventional Pascal operators,
data types and control structures. In addition, Lazarus provides an
extensive palette of VISUAL COMPONENTS, allowing for Rapid Application
Development (RAD) with minimal coding.
If you have a version of Lazarus other than version 1, you can write
Console Applications (Text-based applications) that feature many of
the features of a CRT-based application without some of the CRT routines
(like clrscr, gotoxy etc). If you are interested in doing this, then
you can learn more about Console Applications
for practising standard text-based Procedural Pascal.
General Features
- the programs' data is as important as the instructions (equal
weight of time will be devoted to each in this course)
- all data used in programs is of distinct types (eg. integer, real,
byte, boolean, char, string etc..)
- there are rigid restrictions on how instructions can be written
(syntax)
- it is a small language (with a limited vocabulary of approx 40
reserved words), but capability only limited by skill of programmer
- it is LOGICAL, requires sequential, modular approach to problem
solving (learning this will drive you nuts).
- it is COMPILED (ie. SOURCE code compiled into .EXE files in either
memory, or on disk) which can then be run in Windows. The Compiler
will identify SYNTAX errors, and only SOME LOGICAL ERRORS - it is
the programmers job to construct a correct program (as compared
with one that compiles OK but gives the wrong answers)
SYNTAX: rules for allowable statement formation (lines of
code)
CONVENTION: established practices affecting the appearance,
organisation and readability of a program (SHOULD be obeyed)
Shortcuts to reduce program size and speed up program are
outside scope of this course (and fall into the topic of optimization or hacking).
Rather we will aim for consistency, readability, problem solving skills
and good programming skills that do not need to be hacked.
General Unit Outline:
procedure procedurename; {Environment}
uses libraryname, unitname; {included resources}
var declarations {Store}
begin
statements {Body}
end;
A Programs Environment:
A windows application is bathed in many layers of resources, including
graphical resources central to the operating environment + functionality
of the operating system + predefined data types + predefined constants
(eg. pi) + reserved words + predefined routines (eg. input/output).
Programmers can add to this environment in any of the above areas
with self written components.
Lazarus Identifiers (names for things)
Identifiers can be variable names, program names, procedure/function names,
object names or names for properties and events associated with those
objects
SYNTAX: letter {letter | digit | underscore}
NOTES: RESERVED words cannot be used as identifiers and an identifier must be <127 characters long
EXAMPLES: V, A2_VARIABLE, a_long_variable
Lazarus IS NOT case specific i.e. a3 same as A3 for a name
CONVENTIONS:
- use meaningful identifiers (AVOID single letters which tell nothing
of the values they represent)
- avoid very long identifiers - difficult to read and a pain in
the **** to type.
- use abbreviations for easy to recognise variables (eg. tot, Avg
etc.)
- for names containing multiple words, capitalise the first letter
of each word LastVal
- BE CONSISTENT, don't chop and change.
The Store
- this is where the machine places data your program needs
- you need to declare the TYPE of information (this defines how much
memory space is needed to store it) and its' name (what name we
are going to refer to the VALUE by, which is translated into a memory
location).
SYNTAX: {Var identifier {,identifier} : type;
{identifier {,identifier} : type;}}
example:
Var
aNum, anotherNum : real;
total : integer;
answer : boolean;
ch : char;
Process Body
SYNTAX:
begin
[statement { ;
statement } ]
end;
You should note that statements are SEPARATED by semi-colons (so the last
statement in a program DOES NOT need one). It is possible but unusual (and wasteful of processor cycles)
to have a empty (NULL) body. This is sometimes used for var checking with the compiler.
It is most common to begin each statement on a new line, aligned with statements at the same level process. It is generally NOT acceptable to 'stack' more than one statement
on a line. If, however, the statements are very compact, and relate
to each other, then this rule may be relaxed. Generally, however,
this leads to less readible code, and is frowned upon.
Comments
Many beginning programmers (newbies) undervalue these (or worse, say they will add them later but never do). Comments are important 'notes' that programmers place in their programs
indicating what the program does.
SYNTAX: [ { | (* [ text ] *) | } ]
EXAMPLES:
{ this is a comment }
(* this is also a comment *)
{ this is incorrectly formed *)
Comments are used to explain sections of code. They add nothing to OBJECT code, and do not make the EXE any bigger, so can use as many as we like. They are used to explain unusual methods employed. They can provide references and proofs of methods. The can identify overall purpose of program. They identify the programmer.
THEY ARE NOT OPTIONAL!
Comments if incorrectly applied can cause PROBLEMS.
An OPEN comment marker ({ or (*) causes to look for a
matching CLOSE comment marker (} or *)) - once it finds
one it trashes all in between when making OBJECT CODE (the EXE) - this can have the undesired effect of ditching code you want included.
eg.
{ this is a poorly placed comment *)
statement1;
statement2;
{ another comment here }
statement3;
statement4
The above code will cause statements 1 and 2 to be LEFT OUT of the
OBJECT code at compile time. Can you see why?
Reserved Words
What follows is a list of some of the words that mean something specific in
Lazarus - they cannot be used for anything other than their intended
purpose.
at begin case const downto else end except
finally for forward function if implementation initialization
interface private procedure public raise repeat restrictions
string try type unit until uses var while with
There are many other words known to Lazarus, and standard functions,
procedures and constants, which can be used by a programmer or re-defined.
Be warned, however, that re-defining an existing word in Lazarus effectively
cancels out it's original purpose.
Introducing Lazarus Pascal Syntax