Site hosted by Angelfire.com: Build your free website today!

P A S C A L  is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides a teaching language that highlights concepts common to all computer languages. It standardizes the language in such a way that it makes programs easy to write. Strict rules make it difficult for the programmer to write bad code! Basic format of every Pascal program

In computer science, Pascal is one of the landmark programming languages on which generations of students cut their teeth and variants of which are still widely used today. TeX and much of the original Macintosh operating system were written in Pascal. Pascal is based on the Algol programming language.

Pascal is an ordinary high-level language which is widely used as a starting or as a teaching language. However, nowadays Pascal is sometimes preferred to other languages, and could be useful to solve technical problems. It is not necessary to learn any other easier language to start learning Pascal, or any of that sort. It is a very easy programming language and helps you to understand the basics of the world of programming. Also, it greatly  helps you in order to start learning the C programming language. I had different experiences, one of which I learned Pascal, and then moved to C programming very easily. The Pascal programming language has its structure and syntax very similar to that of the C programming language.

Every Pascal program has the same essential format, which is illustrated below,

program TITLE ( input, output );

begin

  program statements;

  program statement

end.

program  is the first word of all Pascal programs. It is a keyword (Keywords are reserved, i.e., you cannot use keywords to describe variables).

TITLE  is the name the programmer gives to the Pascal program being written. It is an identifier. Identifiers begin with a letter, then followed by any digit, letter or the underscore character ( _ ).

(input, output)  states what the program will do, i.e., input and/or output data. Data is inputted from the keyboard, and outputted to the console screen.

begin  defines the starting point of the program, and provides a means of grouping statements together (ie all statements between a begin and end are considered part of the same group or block).

program statements  are commands or instructions to the computer which perform various tasks.

end.  This must always be the final statement of a Pascal program.

"ALL PROGRAM STATEMENTS AND LINES ARE TERMINATED WITH A SEMI-COLON, EXCEPT THE BEGIN AND END KEYWORDS. PROGRAM STATEMENTS PRECEEDING AN END STATEMENT DO NOT REQUIRE A SEMI-COLON."

The Pascal language was named for Blaise Pascal, a French mathematician who was a pioneer in computer development history. In 1641, at the age of eighteen, Pascal constructed the first arithmetical machine, arguably the first computer. He would improve upon the instrument eight years later. In 1650, Pascal left the world of geometry and physics, and shifted his focus towards religious studies, or, as Pascal wrote, to "contemplate the greatness and the misery of man." Pascal died in Paris on August 19, 1662.

The earliest computers were programmed in machine code and assembly. This type of programming is tedious and error prone, as well as extremely difficult to understand and modify. Programming is a time-consuming and expensive process. High level languages were developed to resolve this problem. High level languages provide a set of instructions that read like English, but can be translated by a program called a compiler into machine code. Pascal is one such language.

Other high level languages developed in the early years of the computer were FORTRAN (1957), COBOL (1959), ALGOL (1960), APL (1962), BASIC (1964), C (1972) and Ada (1983), to name a few. One problem with many of the early languages (e.g., FORTRAN and BASIC) was the heavy dependency on the use of "goto" instructions. "Goto" instructions tell the computer to jump from one step to another, enabling the computer to skip steps or to go back to repeat earlier steps. This type of sporadic branching increases the difficulty of debugging code. Additionally, languages like COBOL were designed with over-elaborate definitions, weak data structures support, and a lack of flexibility, making programs tedious to code and difficult to enhance.

Niklaus Wirth completed development of the original Pascal programming language in 1970. He based it upon the block structured style of the Algol programming language. There were two original goals for Pascal. According to the Pascal Standard (ISO 7185), these goals were to a) make available a language suitable for teaching programming as a systematic discipline based on fundamental concepts clearly and naturally reflected by the language, and b) to define a language whose implementations could be both reliable and efficient on then-available computers.

Pascal went far beyond its original design goals, with commercial use of the language often exceeding academic interest. Pascal provides rich data structures, including both the enumerated and record data types, and defined with a pleasing and powerful clarity. It provided an orthogonal and recursive approach to data structures, with arrays of arrays, arrays of records, records containing arrays, files of records, files of arrays, files of records containing arrays of records, and so on. Pascal's popularity exploded in the 1970's, as it was used in writing both system and application software. For this reason, the International Standards committee decided that a formal standard was needed to promote the stability of the Pascal language (the ISO 7185 Pascal Standard was originally published in 1983). By the end of the 1970's, more than 80 computer systems had Pascal implementations in use.

One of the more popular Pascal's of the 1970's and early 1980's was UCSD Pascal on the UCSD P-System operating system. The UCSD P-System was developed at the Institute for Information Studies at the University of California - San Diego, under the direction of Kenneth Bowles. In fact, the P-System operating system itself was written in UCSD Pascal. As Wirth writes in his 1985 Turing Award Lecture, From Programming Language Design To Computer Construction, "But Pascal gained truly widespread recognition only after Ken Bowles in San Diego recognized that the P-system could well be implemented on the novel microcomputers. His efforts to develop a suitable environment with integrated compiler, filer, editor, and debugger caused a breakthrough: Pascal became available to thousands of new computer users who were not burdened with acquired habits or stifled by the urge to stay compatible with software of the past."

In 1978, Richard Gleaves and Mark Allen, working on-campus in San Diego, used UCSD Pascal to develop the 6502 interpreter which became the basis for Apple Pascal. By the 1980's, Pascal was used by most universities to teach programming, while still invading the commercial markets. It became so popular that even FORTRAN began to change, taking advantage of Pascal's innovations.

Due to the strong popularity of the Pascal language in system and application software development, and in response to the many cited drawbacks of the original Pascal implementation, an Extended Pascal evolved to address the needs of commercial development. In 1990, the ISO 10206 Extended Pascal Standard was published to support this new version of the language.

In addition to Extended Pascal, in 1986, Apple Computer released the first Object Pascal implementation, a version of its Apple Pascal that supported object-oriented programming. In 1993, the Pascal Standards Committee published an Object-Oriented Extensions to Pascal technical report which was based upon Apple's Object Pascal implementation.

Past Criticism

While very popular (although more so in the 1980s and early 1990s than at the time of writing), early versions of Pascal have been widely criticised for being unsuitable for "serious" use outside of teaching. Brian Kernighan, co-creator of the C programming language, outlined his most notable criticisms of Pascal as early as 1981, in his paper Why Pascal Is Not My Favorite Programming Language. Since that time Pascal has continued to evolve and most of his points do not apply to current implementations.

Many uninformed people still subscribe to the old belief that Pascal is not for "serious" programming and do not realize the benefits it currently offers. This stigma, more than any actual deficiency, is Pascal's biggest liability.

 

Pascal contains some significant language features that allow it to used as a powerful learning tool in introducing structured programming techniques to students :

Built in Data Types - Pascal contains it's own built in data types of Integer, Real, Character, and Boolean.

User defined Data Types - Has the ability to define scalar types as well as subranges of those data types.

Provides a defined set of Data Structures - These data structures include Arrays, Records, Files and Sets.

Has a strong data typing element - Pascal compliers can diagnose an incompatible assignment of one type to a variable to another type.

Supports Structured Programming - This is accomplished through the use of subprograms called procedures and functions.

Simplicity and Expressivity - Because the language is simple and expressive in nature it allows for effective teaching of computer programming techniques.

For Further reading,

Pascal and C

Pascal was developed around the same time as C, and there are important similarities between the two. Original Pascal and straight C are both small, procedural languages implementing structured programming concepts. Both have functionality for the dynamic allocation of memory and some form of pointer manipulation. But the languages have a distinctly different appearance, with C being much more terse. One difference that is a source of holy wars is Pascal's use of := for assignment, with = used for comparison. This contrasts with C's use of = for assignment and == for comparison. Because = effectively serves both purposes in mathematics, people often use that shorter symbol when the other one (either := in Pascal or == in C) is really what is wanted. The designers of C argued that assignment is much more common than comparison, so that it should have the shorter symbol. Pascal supporters argue that accidentally making an assignment can be much more harmful than accidentally performing a comparison, which is certainly true if, as in C, an assignment is perfectly legal inside the test of an if statement.

This debate reflects the differences in design philosophy of the two languages. Pascal was designed, in part, as a teaching language. Error-prone constructs were carefully avoided, and an effort was made to make the syntax easy to understand. The authors of C placed more of an emphasis on brevity.

Another major difference is that Pascal is strongly typed. This means that all variables must be defined with a specific type before they can be used. Also, incompatible variable assignments are not allowed without an explicit type-cast. This prevents common errors where variables are used incorrectly because the type is unknown. It also alleviates the need for Hungarian notation - the practice of prefixing variable names with type-identifying letters.

Unlike C, Pascal permits nested function definitions. In its original form, Pascal lacked a mechanism for separate compilation or for handling arrays with a size unknown at compile time, but for decades versions of the language have been used that had alleviated or eliminated these problems.

 

The Prime area of application that Pascal entails is the learning environment. This language was not really developed to be used for anything other than teaching students the basics of programming; after all it was originally developed for this purpose. In the early 1970's to the early 1990's Pascal was the language of choice for most major colleges and universities for teaching college level programming techniques. Now with the growing popularity of Object Orient Programming Pascal has taken a back seat to other languages such as C++ and Visual Basic.

Implementations

Early approaches (most notably the UCSD p-System) translated Pascal code into a machine-independent p-Code representation. This intermediate code was then interpreted by a program specific to each architecture. As a consequence, only the small interpreter part had to be ported to many architectures. In the 1980s Anders Hejlsberg wrote the Blue Label Pascal compiler for the Nascom-2. Later he went to work for Borland and rewrote his compiler to become Turbo Pascal for the IBM PC. This new compiler sold for $49, which was much less than the price Hejlsberg originally asked for the Blue Label Pascal compiler.

The inexpensive Borland compiler had a large influence on the Pascal community that began concentrating mainly on the IBM PC in the late 1980s. Many PC hobbyists in search of a structured replacement for BASIC used this product. Turbo Pascal, being available only on one architecture, translated directly to Intel 8088 machine code, making it much faster than interpreted schemes.

During the 1990s compilers that could be re-targeted to different hardware architectures became more prevalent. This allowed for Pascal translation to native machine code that was at the same time easily ported to new hardware.

With Turbo Pascal version 5 Borland added object orientation to Pascal.

However, Borland later decided it wanted more elaborate object-oriented features, and started over in Delphi using the Object Pascal draft standard proposed by Apple as a basis. (This Apple draft isn't a formal standard yet.) Borland also called this Object Pascal in the first Delphi versions, but changed the name to Delphi programming language in later versions. The main changes compared to the older OOP extensions were a reference-based object model, virtual constructors and destructors, and properties. There are several other compilers implementing this dialect: see Delphi programming language.

Publicly available Compilers

Several Pascal compilers are available for the use of general public:

GNU Pascal Compiler (GPC) is an additional front-end to the GNU Compiler Collection (GCC), and is written in C.    Distributed freely under the GNU General Public License. The Borland/Turbo Pascal support is somewhat limited though.

FreePascal is written in Pascal (so that it compiles itself), and is aimed at providing a convenient and powerful compiler, able both to compile legacy applications and to be the means of develop new ones. Also distributed freely under the GNU GPL. It can mix Turbo Pascal with Delphi code, and supports a lot of platforms and operating systems.

Turbo Pascal was the dominant Pascal compiler for PCs during the 80s and early 90s, popular both because of its powerful extensions and extremely low compilation times. Currently, older versions of Turbo Pascal (up to 5.5) are available for free download from Borland's site (registration required).

Delphi is Borland's flagship RAD (Rapid Application Development) product. It uses the Delphi programming language, descended from Pascal, to create applications for the windows platform. The latest version also supports compiling to the .NET platform

Kylix is Borland's newest reiteration of the Pascal branch of their products. It is the descendant of Delphi, with support for the Linux operating system and an improved object library. The compiler and the IDE are available now for non-commercial use. The compiler (but not the library or the IDE) is supposed to become Open Source software some time soon.

 

[ Program 1 ] [ Program 2 ] [ Program 3 ]

In a program, you must always obey the rules of the language, in our case, the Pascal language. As you go along the lessons, you must note what you can do and what you cannot do in writing a Pascal program. A very simple program is shown below:   

PROGRAM 1

Program Pascal_Program1A;  

Begin   

 Write('Hello World. Prepare to learn PASCAL!!');

 Readln;   

End.     

The program is written only to display the message : 'Hello World. Prepare to learn PASCAL!!'. This is simply shown on the screen. So, to display any message on the screen, you should use 'write' (or 'writeln'). The 'readln' statement, here is used as to 'stop' the program and wait until the user presses enter. If the 'readln' statement is missing in this program, then the message is displayed on the screen without giving any chance for the user to read it! Try running this program with and without the 'readln' statement and notice the difference. I suggest you see it!! Now, look at this:

Program Pascal_Program1B;begin Write('Hello World. Prepare to learn PASCAL!!');Readln;End.

This program also runs perfectly as the previous one. The only difference is: neatness and friendliness. 

A program in Pascal always starts by the reserved word 'Program' following the title of the program. There are various restrictions on how to write this statement. Below is a simple example of a small program. (Remember: you can copy and paste the program onto your own compiler. Then you may run it.)

The computer must prompt the user to enter a number, then the latter is added to the second number input by the user.  

PROGRAM 2

Program Pascal_Program2;

Var      

    Num1, Num2, Sum : Integer;

Begin       {no semicolon}

 Write('Input number 1:');

 Readln(Num1);

 Writeln('Input number 2:');

 Readln(Num2);

 Sum := Num1 + Num2; {addition}

 Writeln(Sum);

 Readln;

End. 

Now we must take a look at the program. A program in Pascal must always start with the reserved word 'Program' and end with 'End', following a full stop. A full stop is never used within the program, except at the end of the program as seen in the example above.

The 'Var' statement, is used to introduce any suitable variables which must be used, later in the program. These variables are non-constant terms so that they are used in the program for storing purposes. The terms 'Num1', 'Num2' and 'Sum' in the program are the variables which store any numbers, except those which are real (in fact, during the running of the program, a runtime error may occur if a decimal number is input). As you can see in the example above, these variables are assigned to as integers. The term 'integer' means any whole number, i.e. a number which is not a decimal number. The variables 'Num1', 'Num2' and 'Sum' are terms which are not reserved words, but can be used as variables in the program to store data in them. They could be changed more than once. Moreover, I could have used 'number1', 'number2' and 'totalsum' (note that there must be no spaces within the variables), instead of 'Num1', 'Num2' and 'Sum', respectively. As you can see, it is much better to shorten the variables than writing long words, such as 'variable_number1'.

After writing all the variables which are needed to be used later in the program, the main program always starts with the reserved word 'Begin'. Without this word, the compiler will display a diagnostic (error message). In the program above, both of the two types of 'write' are used. These are 'write' and 'writeln'. Both has the same function, except that the 'write' function, does not proceed to the following line when writing a statement. If you run this program, you will notice the difference between them. When using these two terms, any message that will be typed in between the brackets and the inverted commas '(' ')', is displayed on the screen. However, if a variable is used instead of a message, without using the inverted commas, the CPU will display the stored variable in the memory, on the screen. In line 9, the CPU will not display 'Sum' on the screen, but the stored number in the memory. Another important thing which must be noticed, is the semi-colon (;). The semicolon is used after every statement in the program, except those that you will learn later. However, in the example above, there isn't a semicolon after a 'begin' statement. This is because the flow of the program has just started and must not be stopped by a ';'.

The messages in between the braces ({ }) are called comments or in-line documentation. I guess you consider the comments to be 'extra'. Very long programs which include thousands of lines, have already been felt in need of describing certain functions or even complicated functions. In my experiences, I have already met many problems, when refusing to write a program for a long time, and then resuming again writing it! I've made a long time trying to understand what I have done. You must get it into your mind that comments within the braces are not read or compiled by the compiler/interpreter.

The 'readln' statement is another reserved word for input purposes, which enables the user to input a number or text only i.e.: using the keyboard. But in our case the 'readln' statement is used to input numbers only and store them in the variables 'Num1' and 'Num2'. This is because both variables are assigned to as integers, and integer variables, do not store strings. If a string or character is keyed in instead of a whole number, where an integer variable is waiting for user-input, will cause a run-time error. A run-time error is detected by the OS (Operating System; ex. Windows or Linux). Note that a decimal number is also considered as a wrong input; a decimal number must not be input, since it is a real number (more on this later).

After the prompts and inputs by the user, follows the addition. i.e. 

Sum := Num1 + Num2;

The 'return' of the above function is the addition of the values stored in variables 'Num1' and 'Num2'. The important thing that you should know is that one cannot make the same function as follows:

Num1 + Num2 := Sum;

This is another syntax error. It is the fact that transfer of information is from left to right and not from right to left. So, mind not to make this error. The ':=' is called the assignment statement. 

PROGRAM 3

Comments are inserted into Pascal programs by enclosing the comment within { and } braces. Comments are ignored by the computer, but are helpful to explain how the program works to other programmers.

program DEMOPROG (output);

begin

write('Hello there.'); {the write statement does not set the cursor to the beginning of the next line. }

writeln('This is getting boring.') { This is printed on the same line as Hello there, but now the cursor moves to the beginning of the next line, because this time we used writeln  instead of write  }

end.