If you have a Pascalite, or the software emulation, then there's a separate tutorial for you, covering much of the ground as presented here for the FPC/ TP crowd.
Pascal comes with a lot of "built in" words, e.g. Begin, End, Program, "If.. Then... Else, Repeat... Until, and LOTS more that you will meet in future tutorials.
Nonetheless, in the course of many projects, you will find yourself feeling that further words would be useful. Fear not! You can "make up" words, and add them to the language!
Our first example is going to look trivial, but please bear with me for a moment.
In an early tutorial we had the following program
program hi; begin writeln('Hi'); writeln('Bye'); end.
I'm going to change that slightly, blending into it things which should be familiar. The following listing will let you type almost anything, and then you'll get a "Hi" or a "Hello", depending on whether you entered 1 or more characters. And if you enter 999, the program will end. (Ordinarily, you would prompt users, so that they would have a better idea of what is going on, but we'll keep this "lean".)
program UserDefProcs; var sTmp:string; begin repeat readln(sTmp); if length(sTmp)=1 then writeln('Hi')//no ; here else writeln('Hello'); until sTmp='999'; writeln; writeln('Bye. Press the Enter key.'); readln; end.
I hope that is pretty transparent?
If you knew about user-defined procedures, you could achieve the same result with...
program UserDefProcs; var sTmp:string; procedure SayHi; begin writeln('Hi'); end; procedure SayHello; begin writeln('Hello'); end; begin //main repeat readln(sTmp); if length(sTmp)=1 then SayHi//no ; here else SayHello; until sTmp='999'; writeln; writeln('Bye. Press the Enter key.'); readln; end.
At first glance, this might not seem an improvement.... but once you know your way around the basic structure, it isn't bad, and as your programs become more sophisticated, the breaking up of the program into manageable chunks becomes essential. So.... WHAT IS that "basic structure" you need to know your way around?
begin //main repeat readln(sTmp); if length(sTmp)=1 then SayHi//no ; here else SayHello; until sTmp='999'; writeln; writeln('Bye. Press the Enter key.'); readln; end.
"SayHi" and "SayHello" are not standard Pascal words. They are words we have created in order to do the job we have in mind for this program. The names should tell us what they do. We can look at the details in the part of the program before the main part. It is a classic "divide and conquer" strategy.
Remember that you can insert new lines and spaces whereever it helps to make something more clear. Notice how the indenting and blank lines help make the sections of the code more evident. The "then" and "else" which go with the "if" are easy to find. The "until" for the "repeat" is easy to find. the definitions of the two words we made up (before "begin //main") stand out nicely... at least they will, when you get used to reading this stuff!
Let's alter the program so that a blank line and then a line with "===="s appears before each write of "Hi" or "Hello". First we'll do it the dumb, obvious way.
program UserDefProcs; var sTmp:string; procedure SayHi; begin writeln; writeln('==============='); writeln('Hi'); end; procedure SayHello; begin writeln; writeln('==============='); writeln('Hello'); end; begin //main repeat readln(sTmp); if length(sTmp)=1 then SayHi//no ; here else SayHello; until sTmp='999'; writeln; writeln('Bye. Press the Enter key.'); readln; end.
The following illustrates that you can even use words you've made up in words you've made up!
program UserDefProcs; var sTmp:string; procedure separator; begin writeln; writeln('==============='); end; procedure SayHi; begin separator; writeln('Hi'); end; procedure SayHello; begin separator; writeln('Hello'); end; begin //main repeat readln(sTmp); if length(sTmp)=1 then SayHi//no ; here else SayHello; until sTmp='999'; writeln; writeln('Bye. Press the Enter key.'); readln; end.
The only restriction is that the "procedure separator begin.. end" stuff must appear BEFORE any place you try to use your new word (proper term for "your new word": User Defined Procedure). Even in this "trivial" example of the technique, I think you can see an advantage: It saves you typing out the two parts of the "Separator" twice. Half the chance for typos. If you do make a typo, at least the program will run consistently, with the same "Separator" between "Hi"s and "Hello"s. And a big advantage: If you want to make a change... say use "***" instead of "===", you can... without having to go through the program, and make the same change in several places. Within the definitions of SayHi and SayHello, instead of the longwinded....
writeln; writeln('===============');
... we now just have...
separator;
There's more to using procedures than you might anticipate from this brief and limited introduction. They are important because they simplify keeping a project under control. We'll explore them further in future tutorials.
|
Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.
....... P a g e . . . E n d s .....