INTRODUCTION IF YOU KNOW NOTHING ABOUT PASCAL ____________________________________________________________ Assuming 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 on the disks 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 14 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 Page I-1 Introduction 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 (page 37 - version 3.0; page 196 - version 4.0; reference guide page 11 - version 5.x). 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 the 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. Because it would add little and could possibly be confusing, the simple predefined types will not be listed in boldface type. 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 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, hence a much faster execution of the program. This tutorial is written especially for Borland International's TURBO Pascal compilers version 3.0, 4.0, or 5.x. 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. Page I-2 Introduction The decision was made to use the Borland extensions and make the tutorial very 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 a larger 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 definition 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 files will only work properly with TURBO Pascal version 3.0 and some will only work properly with version 4.0 and later, but most will work with either. It will be clearly indicated to you which files will work with any of the versions of TURBO Pascal. It should be pointed out that each succesive 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. WHAT ABOUT TURBO PASCAL VERSION 2.0? ____________________________________________________________ Most of the files will compile properly with TURBO Pascal version 2.0, but no warning will be given since that version has been superseded for so long. It will pay you to purchase a newer version because of the flexibility. If you choose not to however, this tutorial will work fine in most cases if you follow the instructions for TURBO Pascal version 3.0. WHAT ABOUT TURBO PASCAL VERSION 5.5? ____________________________________________________________ Chapters 15 and 16 of this tutorial are written especially for TURBO Pascal version 5.5 to discuss the use of object oriented Page I-3 Introduction 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. PREPARATION FOR USE OF THIS TUTORIAL. ____________________________________________________________ Copy the example files onto your TURBO working disk 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. Be sure you make a backup copy of the Pascal source disk so you cannot accidentally lose all information on the distribution disk. If you are using TURBO Pascal version 3.0, you should read Chapter 1 of the reference manual to be ready to use this tutorial, and if you are using TURBO Pascal version 4.0, you should read parts of chapters 1, 2, & 11 of your reference manual. TURBO Pascal version 5.x (5.0 or 5.5) users should read chapters 1 and 2 of the User's Guide. You should be familiar with use of the editor supplied with TURBO Pascal before beginning. 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 versions 4.0 and 5.x. 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. Page I-4 Introduction 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 DIRECTORY ____________________________________________________________ There is a directory on the distribution disk named ANSWERS which 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 executable 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. A SPECIAL NOTE FOR THE SHAREWARE VERSION ____________________________________________________________ It is impossible to include the graphics diagrams in chapter 12 in a pure ASCII text. They are therefore omitted from this version of the tutorial. If you need these diagrams, they can be purchased directly from Coronado Enterprises along with your registration. See the READ.ME file on either diskette for more information. Page I-5