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

Pascalite Programming: User Defined Procedures: part 2


We made a start with user defined parameters in a Level One tutorial. In this tutorial, we will use them in a form that you haven't seen before. After this tutorial, there is just one further, similar, form to master.

Have a look at the following. All that stuff about bToShow:=... is just to get a number... 0 to 7. We will then show what the number is with the port d LEDs: One LED will light: d0 for 0, d1 for 1, d2 for 2, etc.

When you run the program on the simulator, be careful not to change the settings of b0, b1 and b2 too quickly, as the program takes a little time to run through so many steps.
program ProcsTwo;
var bToShow:byte;

   procedure ShowOnLEDs (bWhichOne:byte);
     begin
       if bWhichOne=0 then write(portd,1);
       if bWhichOne=1 then write(portd,2);{turn on just second LED}
       if bWhichOne=2 then write(portd,4);{turn on just third  LED}
       if bWhichOne=3 then write(portd,8);{turn on just fourth LED}
       if bWhichOne=4 then write(portd,16);{turn on just fifth LED}
       if bWhichOne=5 then write(portd,32);{... etc}
       if bWhichOne=6 then write(portd,64);
       if bWhichOne=7 then write(portd,128);
     end;

begin {main}

def_out(d0,d1,d2,d3,d4,d5,d6,d7);

repeat
  bToShow:=0;
    if b0 then bToShow:=bToShow+1;
    if b1 then bToShow:=bToShow+2;
    if b2 then bToShow:=bToShow+4;
    {By now there is a number in bToShow. What number that
    will be is determined by whether b0, b1 and/or b2
    is on or off.}

  ShowOnLEDs(bToShow);

until 4=5;

end.
What is going on here? You'll need to concentrate! This is one of those things that is easy to use, but hard to explain. It is also one of those things that is so unlike anything that you are likely to have done before that it is hard to grasp at first. Just before we delve into the details, recall WHY we have procedures: to "divide and conquer". In the main part of the program, the stuff after the begin marked "main", first we put a number in bToShow (don't worry too much about how we did that, by the way. Just believe!) Then we have the single, simple line "ShowOnLEDs(bToShow)". This is calling on our user defined procedure to show on the port d LEDs the number that is in bToShow. Because all of the detail of how that will be done is elsewhere, once "ShowOnLEDs" is working, we can just forget about HOW it works, and concentrate on other matters.

When we used a user defined procedure before, there was no number involved. One example, for instance, cleared the LCD screen. In this tutorial's first example, above, we have the number in bToShow... BUT! when we look at the details of how ShowOnLEDs, "bToShow" is nowhere to be found! In theheader for the procedure declaration, we said
procedureShowOnLEDs (bWhichOne:byte);
In this, we created a way to pass numbers between the "outside world" and the code that defines how the procedure will work. When we USE ShowOnLEDs in the main part of the program, we followed it by a variable's name in parentheses. When the computer goes to the procedure's definition, it brings with it whatever number is currently stored in that variable, AND PUTS THAT NUMBER in the variable in parentheses after the start of the procedure definition. In other words, in this case, the number in bToShow is passed to the variable bWhichOne. this is called "passing a parameter". All the time we are inside the details of ShowOnLEDs, we are using bWhichOne when we want to access the number that was in bToShow at the time we said "ShowOnLEDs(bWhichOne);" Let's have another example. It is pretty weird, and probably wouldn't be written like htis, if it were written at all.. but it does show a user defined procedure and parameter passing. Once the program is running, LED d0 is on or off depending on b0 if a0 is off, and LED d0 is on or off depending on b1 if a0 is on.
program ProcsTwoB;
var boStateOfSwitch:boolean;

   procedure ShowOnLEDs (boOnOff:boolean);
     begin
     if boOnOff then set(d0) else reset(d0);
     end;

begin {main}

def_out(d0);

repeat
 if a0 then begin
   boStateOfSwitch:=b0;
   ShowOnLEDs(boStateOfSwitch);
   end {no ; here}
  else begin
   boStateOfSwitch:=b1;
   ShowOnLEDs(boStateOfSwitch);
   end;

until 4=5;

end.


Note that on the line
procedure <name>(<variable name>:<data type>);
we have that extra bit (:<data type>), rather like in the var statement that is near the beginning of the program. This is because the compiler needs to know what TYPE of data is going to be passed to the procedure.

Notice that when we actually call (or invoke) the procedure we DON't have the extra bit, the (:<data type>). This is logical: in our example above, we already know boStateOfSwitch is holding Boolean type data when we use the variable as part of ShowOnLEDs(boStateOfSwitch); in the "if... then..." and "...else..." clauses.

Really cool, really useful: any variable that you declare in the procedure WhatsIt(bThingie:byte) header line isn't "known" to the rest of the program, even if it has the same name! Example (don't try to run it)
program ProcsTwoB;
var boBadName:boolean;

   procedure ShowOnLEDs (boBadName:boolean);
     begin
     if boBadName then set(d0) else reset(d0);
     boBadName:=false;
     end;

begin {main}

def_out(d0);

repeat
   boBadName:=b0;
   ShowOnLEDs(boBadName);
   if boBadname then write(LCD,'boBadName is true'){no ; here}
     else write(LCD,'boBadName is false');
   end;

until 4=5;

end.
This would run in most Pascals. The message on the LCD would depend on b0, IN SPITE OF THE boBadName:=false IN THE PROCEDURE. This is because only the procedure's boBadName has been made false. The boBadName belonging to the main program is quite separate, and won't have changed.

Pascalite is a little fussy: It won't let you create a second variable with the same name of one already declared, e.g. you can't have two boBadNames. Just remember the way variables get hidden from the main program if you're ever programming in other Pascals. Before you get too indignant about this limitation of Pascalite, just recall how much if costs and the fact that it is a splendid example of slimware, i.e. software that does a useful job without requireing 10 megabytes of disc space and without having 10 megabytes of code for errors to hide in.

The variable boOnOff (part of ShowOnLEDs in early version of the program in this tutorial) doesn't exist outside ofthe procedure. You couldn't, for instance, say "if boOnOff then..." just before the "until 4=5".

Another thing to note: Consider the following pointless program:
program scope;
var bNum:byte;

   procedure Smaller(bNumber:byte);
     begin
     write(LCD,bNumber);
     bNumber:=bNumber-2;{No point, exept to illustrate something.}
     write(LCD,bNumber);
     end;

begin {main}
bNum:=70;
Smaller(bNum);
write(LCD,bNum);
end.
This will cause F, D, abd F again to appear on the LCD. (Which will then immediately go back to its usual "resting" state.)

70 is the code for F, 69 the code for D, so it isn't too hard to see the cause of first F and the subsequent D. Where common sense might suggest that the "write(LCD,bNum);" before the "end." might result in another D, instead we get a second F because then number stored in the variable bNum hasn't changed, even though bNumber was initially filled from bNum and was then diminished by 1.

There IS a way to get something like bNum changed when something like bNumber within a procedure changes. I don't use it very often, but you will be shown how. Pehaps I will use it more in Pasalite because you cannot declare user defined functions, which those readers with Pascal experience might be expecting to use. How to "pass back" those values is covered in part 3 of the parameter tutorial.


To search THIS site.... (Go to the site's above, and use their search buttons if you want to search them.)... Way to search this site without using forms
   Search this site or the web        powered by FreeFind

  Site search Web search

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.

Link to editor's (Arunet) homepage
How to email or write this page's editor, Tom Boyd