The Pascal Programming Language - DOC by zch19242


									The Pascal Programming Language

Zak Bahri

CSC 415

I.     History

To be able to truly explore Pascal programming language, I believe that one must know

some history of this language. The most widely used languages in the 1960s were

FORTRAN and COBOL. These were useful languages but they were poorly structured

and were very difficult to troubleshoot. The coding for these languages was referred to as

“spaghetti code,” where routines jump to and from various parts of the program, reducing

readability and making code hard to use again and hard to debug. In the mid-1960s, a

movement toward structured programming emerged where the strictness in syntax, data

types, and program structure would create languages that were easier to implement into

existing and new systems, and much simpler to maintain in comparison. Thus giving

them longevity and greater application. (Burks. Brighton)

Out of this movement PASCAL was born, a programming language named after the 17th

century mathematician Blaise Pascal. However, its birth was not quite as simple as

naming it.

Pascal grew out of ALGOL, a programming language intended for scientific computing.

Meeting in Zurich, an international committee designed ALGOL as a platform-

independent language. This gave them greater free reign in the features they could put
into it, but also made it more difficult to write compilers for. Many computer vendors did

not, which did not propel the programming language immediately into the record books.

The lack of compilers on many platforms, combined with its lack of pointers and many

basic data types, such as characters, led to ALGOL not being widely accepted. Scientists

and engineers flocked to FORTRAN, a programming language which was available on

many platforms. ALGOL mostly faded away except as a language for describing


In the 1960s, several computer scientists worked on extending ALGOL. One of these was

Dr. Niklaus Wirth of the Swiss Federal Institute of Technology (ETH-Zurich), a member

of the original group that created ALGOL. In 1971, he published his specification for a

highly-structured language which resembled ALGOL in many ways. He named it Pascal

after the 17th-century French philosopher and mathematician who built a working

mechanical digital computer.

Pascal is very data-oriented, giving the programmer the ability to define custom data

types. With this freedom comes strict type-checking, which ensured that data types didn't

get mixed up. Pascal was intended as a teaching language, and was widely adopted as just

that. Pascal is free-flowing, unlike FORTRAN, so student programmers didn't have to

worry about formatting. In addition, Pascal reads very much like a natural language,

making it easier to learn. One of the things that killed ALGOL was the difficulty of

creating a compiler for it. Dr. Wirth avoided this by having his Pascal compiler compile

to an intermediate, platform-independent object code stage. Another program turned this

intermediate code into executable code. Prof. Ken Bowles at the University of California

at San Diego (UCSD) seized on the opportunity this offered to adapt the Pascal compiler
to the Apple II, the most popular microcomputer of the day. UCSD P-System became a

standard, and was widely used at universities. This was aided by the low cost of Apple

II's compared to mainframes, which were necessary at the time to run other languages

such as FORTRAN. This made it a flexible programming language that could be used on

a personal computer. Something that FORTRAN could not offer, flexibility along with

an ease of use, two staples of a successful product.

By the early 1980's, Pascal had already become widely accepted at universities. Two

things happened to make it even more popular.

First, the Educational Testing Service, the company which writes and administers the

principal college entrance exam in the United States, decided to add a Computer Science

exam to its Advanced Placement exams for high school students. For this exam, it chose

the Pascal language. Because of this, secondary-school students as well as college

students began to learn Pascal. Pascal remained the official language of the AP exams

until 1999, when it was replaced by C++, which gave way to Java very soon afterwards.

Second, a small company named Borland International came out with the Turbo Pascal

compiler for the IBM Personal Computer. This compiler was truly revolutionary. It did

take some shortcuts and made some modifications to standard Pascal, but these were

minor and led to its greatest advantage: speed. Turbo Pascal compiled at a dizzying rate:

several thousand lines a minute. At the time, the available compilers for the PC platform

were slow and bloated. When Turbo Pascal came out, it was a breath of fresh air. Soon,

Turbo Pascal became the de facto standard for programming on the PC. When computing

magazines published source code for utility programs, it was usually in either assembly

or Turbo Pascal.
At the same time, Apple came out with its Macintosh series of computers. Since UCSD

Pascal has first been implemented on the Apple II, Apple made Pascal the standard

programming language for the Mac. When programmers received the API and example

code for Mac programming, it was all in Pascal.

         There were some who intended to modify the original Pascal language to suit their

needs, but its true creator dedicated himself to fixing the problems with the original

program. He released Modula-2, essentially a patch of the original hat had the ability to

sub-divide a program into separate modules that could then be compiled with greater

ease. Adding maneuverability to the classic language. Oberon was the last modification

on the original design that is based on OOD (Object-Oriented Design). This has left the

original Pascal language rendered obsolete it is still well suited for teaching as it has

influenced programmers, and programming for decades.(

II.      Design, Syntax, and Semantics

A- Program Structure

      Pascal is structured to be a simple language that was easy to read, understand, and

      edit. The beauty of simplicity was very true of this language. Pascal reads much like

      an essay. It begins with the program heading, the program data definition, and the

      program statement body. The beginning of the program states that it is the beginning

      by using the reserved word “program” then stating the name of the program and all

      input and outputs that will be used as parameters in the program. In the introduction

      or the program data definition section each variable, constant, label, and type

      definition that will be used in the body of the program is declared. This lets the
   reader know what the program is about, and what the reader might be looking for.

   Just like any good essay this section should be well documented for readability. The

   program statement body is housed by a program block which is marked by “begin”

   and “end”. This comes directly from Algol, and is one of the features that make

   Pascal flexible by allowing portions of the program to be sectioned off into


B-Data Types

   In Pascal there are four major data types they are the following:

       Type                     Example              Domain

       Integer        1,2,3,4           1≤x≤∞        x є +I

       Boolean                  True, False          True or False

       Real                     0,56,√2,             -∞ ≤ x ≤ ∞      x єR

       Char                     a,c,z,+,/            ASCII characters

       (Notice that there is no String data type!)

With these data types, we can effectively represent any number or type of

number/character we can think:



<some stuff>


PASCAL‟s syntax rules for assigning values to variables take the following structure:

<identifier_name> := <value>

Pascal has also a type that is referred to as the “enumerated” type.

The integers go from -2147483648 to 2147483647 and the characters can be ASCII or

ABCDIC. (Brown/P Henry 1988-1999)

C- Type Checking

Unlike C-based languages variables of one type in Pascal cannot be assigning to

variables of another type.

If you try to assign a real to an integer it will give you a compilation error.

D-     Assignment Statements and Expressions
 The assignment statement is a simple statement that is used to change the value of a


The general format is:

var := expression

An expression is the symbolic representation of a mathematical or logical statement. It's

operators are any of the accepted Pascal operators, and the operands can be constants,

variables, functions, or other expressions. When an expression is evaluated, no matter

how complex, it must always yield a single value as a result. Expressions may appear in

several different kinds of statements. When used in an assignment statement, the

expression always appears on the right side if the assignment operator (:=).




     a * 10 – c

     a * (10 - c)

     a or b

     ord( let ) + 10

      Although not actually operators, brackets () are used to clarify and sometimes

change the order of evaluation of parts of an expression. They can be used with any type

of operand.

     The following are all ARITHMETIC operators, and are used with the arithmetic

types integer and real.




           / (real only)

           div (integer only)

           mod (integer only)

The '/' operator is used for real division and will produce fractional results, while the 'div'

operator is used with integer operands only and the result is always truncated (for

example: 10 / 4 = 2.5 while 10 div 4 = 2). The 'mod' operator is modulus and its result is

the remainder (10 mod 3 = 1). Note that there is no exponential operator.

     LOGICAL operators expect Boolean operands. The result of a Boolean expression

can only be true or false. There are 3 logical operators whose results are as described in

the truth tables in Section 6
           not (unary operator)



   The last sets of operators are known as the RELATIONAL operators. The operands

may be of any scalar type (such as integer or char) or of type 'real'. Both operands for any

one of these operators must be of the same type, but the result will always be Boolean.

Although sometimes used in assignment statements, relational expressions are most often

used in conditional and repetitive statements, often in conjunction with Boolean operators

(eg. (len > 10) and (len < 100) ).

     = (equality)

     <> (inequality)

     < (less than)

     <= (less than or equal to)

     > (greater than)

     >= (greater than or equal to)

      Another entity that can be used in an expression is a FUNCTION. A function has

a single (scalar, subrange or pointer type) result. Pascal defines a set of pre-defined (or

built-in) functions that are standard to all Pascal implementations.


     abs           round       trunc       sqr

     sqrt          arctan     cos         sin

     exp           ln


     odd           eof        eoln


     trunc         round       ord         chr

     succ          pred

The general form of a function invocation is:

 functioname(arg1, arg2, ..., argn)

The number of arguments required is determined by the individual function. Most have

only one or two.

      There are very specific rules for the order in which elements of an expression are

evaluated, known as the RULES OF PRECEDENCE. These are as follows:

     First: all functions

     Second: anything in brackets ()

          These are evaluated from the inner-most set of brackets out-ward

     Third: not

     Fourth: * / div mod and

     Fifth: + - or

     Sixth: <= = >= > < <> in

     All operators at the same level of precedence are evaluated in the order in which

they appear, from left to right. The use of brackets can change the normal order of

evaluation which can produce very different results. Superfluous brackets are allowed

and are often useful to help clarify lengthy expressions or to help ensure that the

expression is evaluated as desired.

      Mixed mode arithmetic occurs when values of different types are used in the same

expression (i.e. integers and reals mixed). The Pascal run-time system will often handle

this situation but has its own rules about when to truncate real values and when not to, so

results can be unexpected. It is best to avoid mixed-mode arithmetic whenever possible,

but if you must, be very careful to convert necessary values so all are of the same type.

(Pascal tutorial)

E- Control Structures

Pascal has extensive capabilities to do looping and conditional branching:


This is used to repeat a single Pascal statement any number of times we desire.

Loop variable must be an ordinal type of usual scope, the loop variable cannot be

changed in the loop, the loop parameters can be changed, but they are evaluated just

once, so it does not affect loop control.


Compound statements are a group of statements, separated by semicolons that are

surrounded by the keywords Begin and End. The Last statement doesn‟t need to be

followed by a semicolon, although it is allowed. A compound statement is a way of

grouping statements together, executing the statements sequentially. They are treated as

one statement in cases where Pascal syntax expects 1 statement, such as in if ... then


It should be fairly self-explanatory. Any condition that can be reduced to a logical

Boolean (TRUE or FALSE) answer is put between the IF-THEN pair of words. If the

resulting expression is TRUE, then the single Pascal statement following the reserved

word THEN is executed, but if it is to FALSE, then the statement is skipped. (The single

statement can be replaced with a compound statement bracketed with a BEGIN-END


F- Subprograms

 Subprograms help reduce the amount of redundancy in a program. Statements that are

executed over and over again but not contained in a loop are often put into subprograms.

    Subprograms in Pascal are broken up into 2 areas: procedures and functions. Main

tasks should be contained in procedures; so in the main program, you don't have to worry

about the details. This also makes for reusable code. You can just keep your procedures

in one file and link that into your program.

A procedure has the same basic format as a program:

  procedure Name;

   (* Constants *)


   (* Variables *)


   (* Statements *)


To call the procedure from the main program, just use the name.


Procedures are very often used to output data.

   Functions work the same way as procedures, but they always return a value to the

main program separate from the variables passed to the function:

   function Name (parameter_list) : return_type;

Functions are called in the main program by using them in expressions:

   a := Name (5) + 3;

   Be careful not to use the name of the function on the right side of any equation inside

the function because instead of returning the value, as might be expected, this sets up an

infinite recursive loop.

        It is generally bad programming form to make use of VAR parameters in

functions, functions should return only one value. (Pascal tutorial)
G-     Variable Scope

 “Now you see me now you don‟t”

The static scope of a variable is the most immediately enclosing block, excluding any

enclosed blocks where the variable has been re-declared.

In Pascal, the blocks are procedure definitions. The most immediately enclosing block is

the procedure in which the variable is declared. The enclosed blocks are procedures

defined inside this procedure.

Therefore, the static scope of a variable in Pascal is the procedure in which the variable

is declared, excluding any procedures defined inside this procedure that declare another

variable by the same name.

The static scope of a variable in a program can be determined by simply studying the text

of the program. Static scope is not affected by the order in which procedures are called

during the execution of the program.

Only the variables declared before the block program are visible to that program, and the

variables that are declared in a subprogram are only visible until the duration of the

subprogram. (
H.      Missing Features

Niklaus Wirth designed Pascal as an educational programming language.

Consequently, he could (and did) ignore many of the problems encountered in large-

or even medium-scale programming projects. Unfortunately, a large number of

programmers and companies decided to adopt Pascal as a real programming

language, adding features as needed to get around its deficiencies. Its arbitrary

restrictions, missing features, single-pass orientation, and very poor state of

standardization. Pascal, at least in its standard form, is just plain not suitable for

serious programming.( Pascal's Progeny, Canta Forda Computer Laboratory )

I- Exception Handling

Pascal does not support exception handling, and the only way check for something

that can cause a run-time error must be done manually.

J- Object Oriented Programming

Standard Pascal did not have his features, but it was corrected in Object Pascal. A

new data type is added, the object. An object is very much like a record in that it can

have multiple data fields of arbitrary types. In addition, you can specify a list of

procedures and functions, referred to as methods, for a particular object type. These
     methods define the actions that an object of this type can perform. Furthermore, you

     can define an object type that inherits the fields and methods of another object type.

     The new type can define additional fields and methods and can choose to selectively

     override methods that it has inherited. (Math tech object Pascal)

  K- Program Modularity

At first Pascal was designed so it contains the whole program in one file, but a module

was created in extended Pascal, so Pascal can act like a C program and have a main

program and a header file, The module interface begins with „import‟ and „export‟

headings, and if you want to access a variable it has to be declared in the export.

III. Critical Evaluation

     A.     Readability –

     Pascal is a very readable language because it was designed to be an educational tool.

Pascal‟s structure is very basic each program has a header, data definition section, and

statement body. This structure makes it very simple all variables are declared before the

program body and access to variable scopes is very easy.

B.        Writability
Pascal is also easy to write but has only a handful of basic control structures and

operators as a foundation that creates simple programs that are straightforward and

mostly intuitive.

C.        Reliability

Strong type-checking that have made Pascal fairly reliable but it does not support

exception handling, as most modern languages such as Java and C++ do, this greatly

reduces reliability. Pascal also provides the programmer with access to memory pointers,

and all the risks that come with them. This can cause unexpected results, may cause

subtle errors that are difficult to find the root of, and ultimately runtime errors that cause

the program to crash.

     D.        Cost

     Pascal‟s was supposed to be an educational tool so it have little cost.

Conclusion –

Despite its fading away as the de facto standard, Pascal is still extremely useful. In

Pascal, mixing types often led to an error. In C/C++, nothing would happen. You could

even treat pointers as integers and do pointer arithmetic with them. In this way, you could

very easily crash your program.

Another reason: speed and size. The Borland Pascal compiler is still lightning- fast.

Borland has revitalized Pascal for Windows with Delphi, a Rapid-Application-

Development environment. Instead of spending several hours writing a user interface for
a Windows program in C/C++, you could do it in ten minutes with Delphi's graphical

design tools. You could do the same in Visual BASIC, but Delphi is so much faster than

Visual BASIC.

Also, Pascal remains preferred at many universities, partly due to the complexity of

teaching introductory programming in Java or C++. Teaching Java requires the teaching

of handles and object orientation - a lot of overhead for a beginning programming course.

To teach simple procedural programming, Pascal remains the top choice.

Thus, even after C, C++, and Java took over the programming world, Pascal retains a

niche in the market. Many small-scale freeware, shareware, and open-source programs

are written in Pascal/Delphi. So enjoy learning it while it lasts. It's a great introduction to

computer programming. It's not scary like C, dangerous like C++, or abstract like Java. In

another twenty years, you'll be one of the few computer programmers to know and

appreciate Pascal.

IV.     Appendices

A. Pascal Program Structure

program my_program;


... {labels}


... {user-defined types}

... {variable declarations}


... {constant variables}

... {any function and procedure definitions}


.... {statements}


      V.    Bibliography and References

Cambridge University Engineering Department,
“Tutorial Guide to Pascal Programming.”

Catambay, Bill, “The Pascal Programming Language.” Sept. 2001.

Doyle, Ken, “Introduction to Object Pascal.”

GNU Pascal, “The GNU Pascal Manual.” Mar.

Wikipedia, “Pascal Programming Language,”

University of Calgary “ Becker‟s webpage”:
Pascal tutorial:
The Pascal programming language:(Travis cope, fall 2003)
Objects in Pascal:

Pascal programming by Victor Saliba:

To top