Pascal Programming Language In Lazarus 
IDE : Part 1 
By: Luis A. Sierra


Introduction

•  Most  of  the  projects  we  will  do  to  explain  the  use  of  the  language  will  be  in  the  console.  In  this  way,  the  use  of  the  Pascal  language  can  be  understood  natively  so  that  the  reader  can  clearly  understand  the  instructions  of  the  language.

•   Once  the  language  has  been  mastered,  the  use  of  visual  controls  and  their  characteristics  in  a  programming  IDE  for  Pascal  will  be  explained.

•  At  the  end  of use  this tutorial  the reader will  can  create  and  design  an  application  they  want  with the  pascal  language. the reader  will can choose your favorite IDE for create yor  design  application, Lazarus or embarcadero Delphi.


What is Pascal

Pascal is a programming language created by Swiss professor Niklaus Wirth between 1968 and 1969, and published in 1970. His goal was to create a language that would make programming learning easier for his students, using structured programming and data structuring. However, over time its use went beyond the academic field to become a tool for creating applications of all kinds.


History

Before delving into Pascal design, Niklaus Wirth had developed Euler and ALGOL W, and later went on to develop the Pascal-style languages Modula-2 and Oberon.

Initially, Pascal was largely, but not exclusively, intended to teach students structured programming. A generation of students uses Pascal as an introductory language for graduation courses. Variants of Pascal have also been used for everything from research projects to PC games and embedded systems. There are new Pascal compilers that are widely used.

Pascal was the primary high-level language used for development on the Apple Lisa, and in the early years of the Macintosh. Some parts of the original Macintosh operating system are translated by hand into the Motorola 68000 assembly language from its Pascal sources.​

Likewise, Donald E. Knuth's TeX typesetting system was written in WEB, the original literary programming system, based on DEC PDP-10 Pascal.

For their part, applications such as Total Commander, Skype and Macromedia Captivate were written in Delphi (Object Pascal). Object Pascal (Embarcadero Delphi) is still used for Windows application development, but also has the ability to cross-compile the same code for Mac, iOS, and Android.

Another cross-platform version called Free Pascal, with the Lazarus IDE, is popular among GNU/Linux users, as it is also in line with the “write once, compile anywhere” philosophy. CodeTyphon is one of the Lazarus distributions with more pre-installed packages and cross-compilers.


Object Pascal (Licenced)


Object Pascal is an evolution of the Pascal programming language, including elements belonging to the object-oriented programming paradigm.

Object Pascal is an extension of the Pascal language that was developed at Apple Computer by a team led by Larry Tesler in consultation with Niklaus Wirth (inventor of Pascal). It is a descendant of an earlier object-oriented version of Pascal called Clascal, which was available on Lisa's computer.

Object Pascal was necessary in order to support MacApp, an extensible Macintosh application framework that would now be called by a class library. The Object Pascal and MacApp extensions were developed by Barry Haynes, Ken Doyle, and Larry Rosenstein, and were tested by Dan Allen. Larry Tesler oversaw the project, which was started very early in 1985 and became a product in 1986.

One of the extensions to Object Pascal was also carried out in the THINK Pascal IDE. The IDE includes the compiler and an editor with syntax highlighting and checking, a powerful debugger, and a class library. Many developers prefer THINK Pascal over Apple's implementation of Object Pascal, because THINK Pascal offers tight integration of its tools. Development stopped after version 4.01 as the company was acquired by Symantec. So the developers shelved the project.

In 1994 Apple dropped support for Object Pascal when they switched from using 68K Motorola chips to PowerPC, with IBM architecture. MacApp 3.0, for this platform, was rewritten in C++.


Compilers (Licenced)


Some of the compilers that support one or more versions of this language (each with its own particularity) are:

Borland Delphi, which was initially used as the name for its programming language Object Pascal, but later, after making changes to it, it was changed to Delphi.

Embarcadero Delphi (formerly CodeGear Delphi and before that, Borland Delphi) is probably the best-known compiler. It is the successor to the successful product lines.


Database Support

One of the main features and advantages of Delphi is its ability to develop applications with connectivity to databases from different manufacturers. The Delphi programmer has a large number of components to connect, manipulate, present and capture data, some of them released under free or open source licenses. These data access components can be linked to a wide variety of visual controls, taking advantage of the features of the object-oriented language, thanks to polymorphism.

In the components palette you can find several tabs to make a connection to databases using different layers or connection engines.

There are engines that allow you to connect to databases from different manufacturers such as BDE, DBExpress or ADO, which have handlers for the most widespread formats.

There are also direct connection components for a number of specific databases: Firebird, Interbase, Oracle, etc.

Below is a brief summary (still being compiled) of the connection layers available for the most popular databases:

Interbase/Firebird: IBX (InterBase eXpress), IBO (IB Objects), MDO (Mercury Data Objects), *DBExpress, BDE, FibPlus, Zeos

Oracle: DOA (Direct Oracle Access), NCOci8

dBase: BDE

FoxPro: BDE

Paradox: BDE

Microsoft SQL Server: BDE, ADO, *DBExpress

MySQL: Zeos (native), *DBExpress, BDE and ADO (using ODBC)

PostgreSQL: BDE, ADO, ZEOSDBO


Free Pascal (Open Source)


Free Pascal is a free, open source, portable Pascal compiler, which represents an alternative to the commercial Turbo Pascal and Delphi products.

Free Pascal is both a 32-bit and 64-bit compiler for multiple CPU architectures and multiple operating systems.

The compiler implements the Pascal dialects of Borland Turbo Pascal and Delphi, plus some MacPascal constructs. It is available for most popular operating systems.

Free Pascal was initially known as FPK Pascal. Because FPK are the initials of the author, Florian Paul Klämpfl, the FPK Pascal never meant "Free Pascal Kompiler", although many people thought that; anyway, writing "Compiler" with "K" is uncommon in Germany. In late 1997, the name of the project was changed to Free Pascal Compiler (FPC) to avoid this confusion and because of the increasing number of people contributing.

FPC is a reasonably well-documented project, with manuals reaching 1800 pages in total.

The visual parts of the Delphi Libraries (the VCL) and the creation of a visual IDE and RAD are part of a separate project, called Lazarus.

The Free Pascal distribution includes a text-mode IDE similar to Turbo Pascal. Although this IDE was deteriorating for some time due to lack of maintenance, in a common effort most of the bugs were fixed, so it is now in very good shape.

Similar to Turbo Pascal and Delphi, Free Pascal has excellent support for assembly language integration into Pascal code. In addition, it supports multiple architectures and notations.


Compilers (Open Source)

Lazarus is a rapid application development (RAD) tool based on the Object Pascal programming language, available for Windows, GNU/Linux, and Mac OS X operating systems.


Database Support

Lazarus supports several database engines, however the developer must install additional packages to manage each one. The programmer can access the database through code or by dragging components into a visual form. The database components represent fields and are connected by correct property settings to a TDataSource, which represents a table and is also connected to the database components, whether it be TPSQLDatabase, TSQLiteDataSet or some other.

The following database engines are supported:

PostgreSQL requires the PSQL package

dBase and FoxPro can be used without the need for an external server or library using the TDbf component

MySQL works correctly

SQLite, you need only a simple external library and the TSQLiteDataSet or ZeosLib component

Microsoft SQL Server, works with ZeosLib library and freetds.

InterBase / Firebird, native access component, also works with ZeosLib

Oracle also works with ZeosLib

MariaDB, accessible through ZeosLib and to archive folder


IDEs


Variables and Constants

  • Variables
Variables are the "place" where we can store the data that the program needs. In Pascal, variables must be defined before using them.

The variables have four characteristics:

  • A name
  • A type of data
  • A scope of visibility
  • A lifetime

All variables have a name, this name is what we are going to call it by so that it returns its value. Also when declaring it we must assign a data type, for example:

Var name: data type;

If I were programming and it were an integer variable, for example, it would be:

Var a : integer;

The scope of visibility is given by the place where we declare it and the lifetime is given by the scope of visibility.

  • Scope of visibility and lifetime.
  • Constants
Pascal allows the declaration of constants, to name values that do not change during program execution. To declare a constant, you do not need to specify a data type, but only assign an initial value. The compiler will examine the value and use the appropriate data type, automatically.

The way to declare it is the same as the variables, as well as its scope of visibility and lifetime.

Some examples of  Constants:

const

           Thousand = 1000;

           Pi = 3.14;

           AuthorName = 'Luis A. Sierra';


Data Types
  • Data Types
The most used data types, which we will see in our text, are integer for integers, real for real numbers, and string for character strings.

  • Integer ordinal type.
  • Ordinal character types

  • Real types.


  • Date and Time
  • String types


  • Conversion between types.


  •  Conversions between TDateTime types with others

 

Example of Data Types

* Variables

* Assignment
* Convert to String
* Convert to Integer
* Convert to Double

* Convert to Boolean
* Convert to Date
* Convert to Time
* Convert to DateTime
* Format DateTime
* DayName of DateTime

* Convert to ASCII
* String Replace
* String Length
* String Copy




Operators
















Object Properties

It is important to know both the events of each object and its properties, so here I am going to show you a series of general properties that I believe adapt to all possible objects.



Events

An event is the action that is triggered after, for example, pressing a button, events are controlled by the messages that Windows emits, so for example when we press a button, a message is produced, which is received and interpreted by our program that does to trigger the event referred to in that message.

Some of the most common events present in almost all objects are:


Basic Structure

  • Console application

  • Visual Application













Pascal, unlike other languages, is not case-sensitive.
It is the same Pascal and pascal.




The Pascal language has the ability to define new types of data in a program. Programmers can define their own data types using type constructors, such as subrange type, array type, record type, enumeration type, pointer type. The user-defined data type, Most importantly, it is the class, which is part of the object-oriented extensions of Pascal.

If you think that type constructors are common in several programming languages, you are right, but Pascal was the first language to introduce the idea in a formal and precise way.
There are still few languages that have so many mechanisms for defining new types.

  • Definition of subrange





  • Definition of the matrix





  • Definition of record








  • Enumerated type definition

Example Program

  • Types with names and types without names



Note: In Pascal the array indices do not have to start at zero, as they do in C, C++, Java and other programming languages.

If / Else Statement

The if-else statement is used to execute both the true part and the false part of a given condition. If the condition is true, the if block code is executed and if the condition is false, the else block code is executed.



CASE Statement

The CASE expression goes through conditions and returns a value when the first condition is met (like an if-then-else statement)

  • Select a value
  • List of possible values

For Statement

The for statement, also called the for loop, lets you repeat a statement or compound statement a specified number of times.

  • increased


  • decremented


While Statement

A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.

Function

A function is a callable unit that has a well-defined behavior and can be invoked by other software units to exhibit that behavior.

Note: functions return  a value.


Procedure

A Procedure is a method associated with an object, and generally also a message. An object consists of state data and behavior; these compose an interface, which specifies how the object may be used. A method is a behavior of an object parametrized by a user.


Note: Procedure do not return value.


Unit

A unit is a sequence of one or more lines, organized as statements, comments, and directives. A subprogram is either a function or a subroutine, and is either an internal, external, or module subprogram. Specifically, a program unit can be: The main program

Example:

1 - Create a new console program in Lazarus (You know how to do it)
2 - Add a Unit file like i show you below:


3 - Add the function to the Unit like below:


4 - Add the code to Main Unit like below:

Exception handling

Exception is typically used to denote a data structure storing information about an exceptional condition. One mechanism to transfer control, or raise an exception, is known as a throw; the exception is said to be thrown. Execution is transferred to a catch.

Example:


Note:

In visual programming, functions and procedures must include the form class before the name.

Example:

Procedure TForm1.calculate(a: integer; b: integer);

Function TForm1.calculate(a: integer; b: integer);


Tutorial's Files:

Post a Comment

Previous Post Next Post