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 the open source FPC, aka Free Pascal. Most of what you read here should also directly apply if you are using Borland's Turbo Pascal (TP hereafter). It is also available for free. Please send me an email with "complaints" if something doesn't work!

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?



The main part of the program is at the end. It runs from the very last "end" in the program, the one with a period (full stop) after it, back to the "begin" that is the partner of that end. (That's the extent of the main part of the program. It is executed from the "begin" downwards, of course.) It is quite usual to mark the relevant "begin" "//Main", to signify that it is the start of the main block of code. In this case, it is the following....
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.

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. The Google advertisers pay me. I know nothing about them or their services, of course.)



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

AND passes... Valid CSS!


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 .....