HOME - - - - - - - - - Other material for programmers - - - - - - - - - Pascalite Tutorial Table of Contents

Pascalite Programming

User Defined Procedures: Part 1

This version of this tutorial is fine tuned for a Pascalite, or the free software Pascalite emulation. Please send me an email with "complaints" if something doesn't work!

If you have the open source FPC, aka Free Pascal or Borland's Turbo Pascal (TP hereafter), then there's a separate tutorial for you, covering much of the ground as presented here for the Pascalite crowd.



If you click on the Help menu item of the Pascalite software, and from there click on contents, then "programming language", and then "reserved words", you will, I hope, be impressed by all of the words that the language can respond to already.

Nonetheless, in the course of many projects, you will find yourself feeling that further words would be useful. 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
if b0 then write(lcd,'Hi') else write(lcd,'Bye');
end.
I hope that is pretty transparent in most respects?. For those who've come to this without seeing the earlier tutorials, the "if b0" part will be true if a switch on the Pascalite is in one position, false if it is in the other position.

If you knew about user-defined procedures, you could achieve the same result with... (don't try to run it, yet, by the way)
program hi;

   procedure SayHi;
     begin
     write(lcd,'Hi');
     end;

   procedure SayBye;
     begin
     write(lcd,'Bye');
     end;

begin
repeat
if b0 then SayHi else SayBye;
until 4=5;
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?


The main part of the program is at the end. In this case, it is the following....
begin
repeat
if b0 then SayHi else SayBye;
until 4=5;
end.
"SayHi" and "SayBye" 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. The main part of this program might better be written....
begin
  repeat
    if b0 then
      SayHi {no ; here}
      else SayBye;
  until 4=5;
end.
Now... a slightly tedious little work around to solve a problem I don't understand. Remember I said above "Don't try to compile yet"? That's because the program as shown won't work, even though it should. The following, however, WILL compile... I've merely "wrapped" the SayHi in a begin.. end

The work around may not be needed by the time you read this, assuming a Pascalite version greater than 2.60B. I was very impressed by the quick, come-clean, answer I received when I sent an email asking about this matter.
program hi;

   procedure SayHi;
     begin
     write(lcd,'Hi');
     end;

   procedure SayBye;
     begin
     write(lcd,'Bye');
     end;

begin
  repeat
    if b0 then
      begin
      SayHi;
      end {no ; here}
      else SayBye;
  until 4=5;
end.


Let's alter the program so that the LCD is cleared before each write of "Hi" or "Bye". To do this in the obvious way, we'd just put a write(lcd,255); in front of the write in the definition of "SayHi" and in front of the write in the definition of "SayBye". (Now the "begin... end" within the two procedures isneeded.)....
program hi;

   procedure SayHi;
     begin
     write(lcd,255);
     write(lcd,'Hi');
     end;

   procedure SayBye;
     begin
     write(lcd,255);
     write(lcd,'Bye');
     end;

begin
  repeat
    if b0 then
      begin
      SayHi;
      end {no ; here}
      else SayBye;
  until 4=5;
end.
If you'll forgive a slight overkill, the following illustrates that you can even use words you've made up in words you've made up! The only restriction is that the "procedure ClearLCD begin.. end" stuff must appear BEFORE any place you try to use your new word (proper term for "your word": User Defined Procedure). Even in this "trivial" example of the technique, I think you can see an advantage. Within the definitions of SayHi and SayBye, instead of the rather cryptic "write(lcd,255)", we have the much more meaningful "ClearLCD". And if that isn't "meaningful" to you, change the name I used to something that is meaningful!

Besides making the code more clear, the following has another advantage. What if you wanted to move the code to a different machine? I am in fact, genuinely, adapting the code to a parallel tutorial which illustrates the same principles, only in the FPC/ TP dialect of Pascal. Instead of hunting through the code, which would typically be much longer, of course, for every place where I want the screen cleared, I only have to alter what is between the "begin" and "end" of the code that defines what happens when "ClearLCD" is called. "Automatically", all through the program, all the things that were originally "write(lcd,255)" are altered to work however the system I am on needs them to work. Brilliant! And it will get better in future tutorials, in which we'll explore other aspects of "making up your own words."

So... just to finish off... here's the code I talked about a moment ago, in which we use a word we've made up ("ClearLCD") within the definitions of other words we've made up. ("SayHi" and "SayBye")...
program hi;

   procedure ClearLCD;
     begin
     write(lcd,255);
     end;

   procedure SayHi;
     begin
     ClearLCD;
     write(lcd,'Hi');
     end;

   procedure SayBye;
     begin
     ClearLCD;
     write(lcd,'Bye');
     end;

begin
  repeat
    if b0 then
      begin
      SayHi;
      end {no ; here}
      else SayBye;
  until 4=5;
end.

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.

Here's the link, if you are ready to return to the index of Pascal tutorials for another one!



Please also note that I have two other sites, and that the following search will not include them. They have their own search buttons.

My Sheepdog Guides site.
My Arunet site.

Go to the sites above, and use their search buttons if you want to search them.
To search this site....

Search this site or the web powered by FreeFind

Site search Web search
The search engine merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

You can also search this site without using forms.
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.


Want a site hosted, or email? You can also help me if you sign up via this link to 1&1's services. (I wouldn't recommend them unless I was happy after several years as one of their customers, but yes, they do pay me if you use this link! As do the Google advertisers, about whom I know nothing, of course.)



Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Also, I have some of my pages' traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Why do I mention the script? Be sure you know all you need to about spyware.
Editor's Main Homepage
How to email or write this page's editor, Tom Boyd

....... P a g e . . . E n d s .....