Pages

Wednesday, May 18, 2011

pascal turorial


PASCAL TUTORIAL

INTRODUCTION

IF YOU KNOW NOTHING ABOUT PASCAL

Assuming that you know nothing at all about Pascal, and in fact, that you may know nothing about programming in general, we will begin to study Pascal. If you are already somewhat familiar with programming and especially Pascal, you will probably want to skip very quickly through the first few chapters. You should at least skim these chapters, and you should read the remainder of this introduction.

A few comments are in order to get us started in the right direction. The sample programs included with this tutorial are designed to teach you the basics of Pascal and they do not include any clever or tricky code. Nearly all of the programs are really quite dumb as far as being useful programs, but all will teach one or more principles of Pascal. I have seen one tutorial that included a 12 page program as the first example. In fact there were only 2 example programs in the entire tutorial, and it was impossible to glean the essentials of programming from that system. For this reason, I will completely bypass any long programs until the very end of this tutorial. In order to illustrate fundamental concepts used in Pascal programming, all programs will be very short and concise until we reach the last chapter.

LARGER PASCAL PROGRAMS

Chapter 16 has some rather large programs to illustrate to you how to write a large program. It would be a disservice to you to show you all of the constructs of Pascal and not show you how to put them together in a meaningful way to build a large program. After completing all of the fundamentals of Pascal, it will then be very easy for you to use the tools learned to build as large a program as you desire or require for your next programming project.

Another problem I have noticed in example programs is the use of one word for all definitions. For example, a sort program is stored in a file called SORT, the program is named Sort, and various parts of the program are referred to as Sort1, Sort2, etc. This can be confusing since you have no idea if the program name must be the same as the filename, or if any of the other names were chosen to be the same because of some obscure rule not clearly documented. For this reason, the example programs use completely arbitrary names whenever the choice of a name adds nothing to the readability or clarity of a program. As an illustration of this, the first program is named Puppy_Dog. This adds nothing to the understanding of the program but does illustrate that the program name means nothing to the Pascal compiler concerning what the program does.

Due to the fundamental design of the Pascal language, certain words are "reserved" and can only be used for their defined purposes. These are listed in your TURBO Pascal reference manual. All of the sample programs in this tutorial are written with the reserved words in all lower-case letters, and the user variables in lower case with the first letter capitalized since this is becoming an accepted industry standard. Don't worry about what reserved words are yet, they will be completely defined later.

In this tutorial, all reserved words, type names, variable names, and procedure and function names will be listed in boldface type within the text as an aid to the student.

WHAT IS A COMPILER?

There are two methods used to run any computer program that is written in a readable form of English. The first method is to use an interpreter. An interpreter is a program that looks at each line of the "English" program, decides what the "English" on that line means, and does what it says to do. If one of the lines is executed repeatedly, it must be scanned and analyzed each time, greatly slowing down the solution of the problem at hand. A compiler, on the other hand, is a program that looks at each "English" statement one time and converts it into a code that the computer understands directly. When the compiled program is actually run, the computer does not have to figure out what each statement means, it is already in a form that the computer can run directly, resulting in much faster execution of the program.

This tutorial is written especially for Borland International's TURBO Pascal compilers version 5.0 through 7.0. These are very high quality compilers that can do nearly anything you will ask them to do since they are so flexible. The original intent of this tutorial was to write it in such a way that it would be completely generic and usable with any good Pascal compiler. The programmers at Borland included a great many nonstandard aids for the Pascal language and resulted in a very good product that has dominated the market for microcomputers. To completely omit all of the extensions would do those of you with the Borland compiler a real disservice, and to include the extensions would not allow other compilers to be used effectively with this tutorial.

The decision was made to use the Borland extensions which may make the tutorial difficult to use with other compilers. If you have a need to use Pascal with some other compiler, TURBO Pascal is so inexpensive that it would be a wise decision to purchase a copy solely for the purpose of learning the Pascal programming language, then moving to the other compiler on a minicomputer or a mainframe using the accumulated knowledge to very quickly learn the extensions provided by that particular compiler. At any rate, this tutorial will not teach you everything you will ever need to know about Pascal. It will, however, teach you the fundamentals and the advanced features of Pascal, but of even more importance is the knowledge of Pascal terminology needed to progress on your own into more advanced topics of Pascal and programming in general. You will find that experience will be your best teacher.

WHICH VERSION OF TURBO PASCAL?

Some of the example programs will not work with some of the earlier versions of TURBO Pascal. This is primarily due to the fact that object oriented programming capabilities were added to version 5.5, and improved on in later versions. Most of the example programs will work with any version however. It should be pointed out that each successive version of TURBO Pascal has been an improvement over the previous version since additional capabilities have been added, and each new one compiles a little faster and results in smaller but faster executable code than the previous version. Any of the versions of TURBO Pascal can be used to learn to program in Pascal, so whichever version you have on hand will be adequate. Later, when you become more versed in programming techniques, you may wish to upgrade to the absolute latest version.

EARLY VERSIONS OF TURBO PASCAL

Most of the files will compile properly with TURBO Pascal versions 2.0 through 4.0. No warning will be given about which files will not compile with these versions since they have been superseded for so long. If you are still using one of the earlier versions, it would be good for you to purchase a newer version because of the flexibility.

WHAT ABOUT TURBO PASCAL VERSION 5.5 & NEWER?

Chapters 14 and 15 of this tutorial are written especially for TURBO Pascal version 5.5 and newer to discuss the use of object oriented programming and how to use the Borland extensions. Since the topic of object oriented programming is a very large and diverse field of study and only a limited space is available to discuss it in this tutorial, these chapters will give you only a brief overview of what it is and how to use it. You will find 13 complete example programs to get you started in this new and very meaningful endeavor and this introduction should whet your appetite to continue your study in more depth.

If you are using an early version of TURBO Pascal without the object oriented extensions, it would pay you to upgrade so you can learn how to use this new programming method. Object oriented programming has the potential to greatly improve the quality of your code and to reduce the debugging time required.

PREPARATION FOR USE OF THIS TUTORIAL.

Copy the example files into your TURBO Pascal working directory and you are ready to begin, provided of course that you have already learned how to start the TURBO system and how to edit a Pascal file.

If you are not using TURBO Pascal, you will still be able to compile and execute many of these Pascal files, since most of the examples use standard Pascal syntax. There will be some statements used which are unique to TURBO Pascal and will not work with your compiler. This will be especially true when you come to the chapter on standard input and output since this is where most compilers differ. Unfortunately, this is one of the most important aspects of any programming language, since it is required to get data into and out of the computer to do anything useful. You will also find that chapter 13, covering the topic of units, is unique to TURBO Pascal and will not work with any Pascal compilers other than TURBO Pascal.

WHAT ABOUT THE PROGRAMMING EXERCISES?

It is highly suggested that you do the programming exercises after you complete the study for each chapter. They are carefully selected to test your understanding of the material covered in that chapter. If you do not write, enter, debug, and run these programs, you will only be proficient at reading Pascal. If you do the exercises completely, you will have a good start at being a Pascal program writer.

It should also be mentioned that this tutorial will not teach you everything you will ever need to know about Pascal. You will continue to learn new techniques as long as you continue to write programs. Experience is the best teacher here just as it is in any endeavor. This tutorial will teach you enough about Pascal that you should feel very comfortable as you search through the reference manual for some topic. You will also be able to read and understand any Pascal program you find in textbooks or magazines. Although the primary goal of this tutorial is to teach you the syntax and use of Pascal, the most important byproduct is the knowledge of Pascal terminology you will gain. This terminology will enable you to learn even more about Pascal and programming in general.

THE ANSWERS TO PROGRAMMING EXERCISES

The directory /ans contains an answer to each of the programming exercises given at the end of the chapters. You should attempt to do original work on each of the exercises before referring to these answers, in order to gain your own programming experience. These answers are given for your information in case you are completely stuck on how to solve a particular problem. These answers are not meant to be the only answer, since there are many ways to program anything, but they are meant to illustrate one way to solve the suggested programming problem.

The answers are all in compilable files named in the format CHnn_m.PAS where nn is the chapter number, and m is the exercise number. If there is more than one answer required, an A, B, or C is included following the exercise number.

Advance to Chapter 1

Return to the Table of Contents