Monday, February 6, 2017

Pascal - Quick Guide

Pascal - Overview

Pascal is a general-purpose, high-level language that was originally developed by Niklaus Wirth in the early 1970s. It was developed for teaching programming as a systematic discipline and to develop reliable and efficient programs.

Pascal is Algol-based language and includes many constructs of Algol. Algol 60 is a subset of Pascal. Pascal offers several data types and programming structures. It is easy to understand and maintain the Pascal programs.
Pascal has grown in popularity in the teaching and academics arena for various reasons:
  • Easy to learn.
  • Structured language.
  • It produces transparent, efficient and reliable programs.
  • It can be compiled on a variety of computer platforms.

Features of the Pascal Language

Pascal has the following features −
  • Pascal is a strongly typed language.
  • It offers extensive error checking.
  • It offers several data types like arrays, records, files and sets.
  • It offers a variety of programming structures.
  • It supports structured programming through functions and procedures.
  • It supports object oriented programming.

Facts about Pascal

  • The Pascal language was named for Blaise Pascal, French mathematician and pioneer in computer development.
  • Niklaus Wirth completed development of the original Pascal programming language in 1970.
  • Pascal is based on the block structured style of the Algol programming language.
  • Pascal was developed as a language suitable for teaching programming as a systematic discipline, whose implementations could be both reliable and efficient.
  • The ISO 7185 Pascal Standard was originally published in 1983.
  • Pascal was the primary high-level language used for development in the Apple Lisa, and in the early years of the Mac.
  • In 1986, Apple Computer released the first Object Pascal implementation, and in 1993, the Pascal Standards Committee published an Object-Oriented Extension to Pascal.

Why to use Pascal?

Pascal allows the programmers to define complex structured data types and build dynamic and recursive data structures, such as lists, trees and graphs. Pascal offers features like records, enumerations, subranges, dynamically allocated variables with associated pointers and sets.
Pascal allows nested procedure definitions to any level of depth. This truly provides a great programming environment for learning programming as a systematic discipline based on the fundamental concepts.
Among the most amazing implementations of Pascal are −
  • Skype
  • Total Commander
  • TeX
  • Macromedia Captivate
  • Apple Lisa
  • Various PC Games
  • Embedded Systems

Pascal - Environment Set Up

There are several Pascal compilers and interpreters available for general use. Among these are −
  • Turbo Pascal − provides an IDE and compiler for running Pascal programs on CP/M, CP/M-86, DOS, Windows and Macintosh.
  • Delphi − provides compilers for running Object Pascal and generates native code for 32- and 64-bit Windows operating systems, as well as 32-bit Mac OS X and iOS. Embarcadero is planning to build support for the Linux and Android operating system.
  • Free Pascal − it is a free compiler for running Pascal and Object Pascal programs. Free Pascal compiler is a 32- and 64-bit Turbo Pascal and Delphi compatible Pascal compiler for Linux, Windows, OS/2, FreeBSD, Mac OS X, DOS and several other platforms.
  • Turbo51 − It is a free Pascal compiler for the 8051 family of microcontrollers, with Turbo Pascal 7 syntax.
  • Oxygene − It is an Object Pascal compiler for the .NET and Mono platforms.
  • GNU Pascal (GPC) − It is a Pascal compiler composed of a front end to GNU Compiler Collection.
We will be using Free Pascal in these tutorials. You can download Free Pascal for your operating system from the link: Download Free Pascal

Installing Free Pascal on Linux

The Linux distribution of Free Pascal comes in three forms −
  • a tar.gz version, also available as separate files.
  • a .rpm (Red Hat Package Manager) version.
  • a .deb (Debian) version.
Installation code for the .rpm version::
rpm -i fpc-X.Y.Z-N.ARCH.rpm
Where X.Y.Z is the version number of the .rpm file, and ARCH is one of the supported architectures (i386, x86_64, etc.).
Installation code for the Debian version (like Ubuntu):
dpkg -i fpc-XXX.deb
Where XXX is the version number of the .deb file.
For details read: Free Pascal Installation Guide

Installing Free Pascal on Mac

If you use Mac OS X, the easiest way to use Free Pascal is to download the Xcode development environment from Apple's web site and follow the simple installation instructions. Once you have Xcode setup, you will be able to use the Free Pascal compiler.

Installing Free Pascal on Windows

For Windows, you will download the Windows installer, setup.exe. This is a usual installation program. You need to take the following steps for installation −
  • Select a directory.
  • Select parts of the package you want to install.
  • Optionally choose to associate the .pp or .pas extensions with the Free Pascal IDE.
For details read: Free Pascal Installation Guide

Text Editor

This will be used to type your program. Examples of few editors include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi.
Name and version of text editor can vary on different operating systems. For example, Notepad will be used on Windows and vim or vi can be used on windows as well as Linux or UNIX.
The files you create with your editor are called source files and contain program source code. The source files for Pascal programs are typically named with the extension .pas.
Before starting your programming, make sure you have one text editor in place and you have enough experience to write a computer program, save it in a file, compile it and finally execute it.

Pascal - Program Structures

Before we study basic building blocks of the Pascal programming language, let us look a bare minimum Pascal program structure so that we can take it as a reference in upcoming chapters.

Pascal Program Structure

A Pascal program basically consists of the following parts −
  • Program name
  • Uses command
  • Type declarations
  • Constant declarations
  • Variables declarations
  • Functions declarations
  • Procedures declarations
  • Main program block
  • Statements and Expressions within each block
  • Comments
Every pascal program generally has a heading statement, a declaration and an execution part strictly in that order. Following format shows the basic syntax for a Pascal program −
program {name of the program}
uses {comma delimited names of libraries you use}
const {global constant declaration block}
var {global variable declaration block}

function {function declarations, if any}
{ local variables }
begin
...
end;

procedure { procedure declarations, if any}
{ local variables }
begin
...
end;

begin { main program block starts}
...
end. { the end of main program block }

Pascal Hello World Example

Following is a simple pascal code that would print the words "Hello, World!":
program HelloWorld;
uses crt;

(* Here the main program block starts *)
begin
   writeln('Hello, World!');
   readkey;
end. 
This will produce following result −
Hello, World!
Let us look various parts of the above program −
  • The first line of the program program HelloWorld; indicates the name of the program.
  • The second line of the program uses crt; is a preprocessor command, which tells the compiler to include the crt unit before going to actual compilation.
  • The next lines enclosed within begin and end statements are the main program block. Every block in Pascal is enclosed within a begin statement and an end statement. However, the end statement indicating the end of the main program is followed by a full stop (.) instead of semicolon (;).
  • The begin statement of the main program block is where the program execution begins.
  • The lines within (*...*) will be ignored by the compiler and it has been put to add a comment in the program.
  • The statement writeln('Hello, World!'); uses the writeln function available in Pascal which causes the message "Hello, World!" to be displayed on the screen.
  • The statement readkey; allows the display to pause until the user presses a key. It is part of the crt unit. A unit is like a library in Pascal.
  • The last statement end. ends your program.

No comments:

Post a Comment