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

Pascalite Programming: User Defined Procedures: part 3


In an earlier tutorial, I presented the following little program of no practical use:
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, and F again to appear on the LCD. (Which will then immediately go back to its usual "resting" state.)

The point of the program was to show that what happened to stuff passed to procedures as parameters does not get passed back to the calling program.... usually! There is a way to make that happen when you want it to. Simply adding three little letters on the third line of the program above will make it display FDD instead of FDF. The reason that it displays FDD is that what is in bNumber at the end of executing the procedure's code whatever's in bNumber is passed back to the variable that held the number passed to the procedure when it was called.

program scope;{revised}
var bNum:byte;

   procedure Smaller(var 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, obliquely, brings me to functions and user defined funtions. Functions are things that "boil down" to something else.

Assuming you have declared a variable bNum, and you can say...
bNum:=random;
... and after the program has executed that line, bNum will equal something between 0 and 255, inclusive. Delphi has many, many, many built in functions... too many, I think, but then again I'm a Luddite. the following are NOT present in Pascalite, but might help you better understand what a function is. If you remember that I would only call a byte-type variable bNum and you spot the problem that some of the examples return data of non byte-type data, overlook it!)
bNum:=sin(27); {Puts the trig sine of 27 into bNum}
bNum:=sin(bANumber);{Does the same thing if you've first put 27 into bANumber}
.... all of the following, as almost everywhere in Pascal, allow you to put a number or a variable holding a number, after the function, as illustrated in the sine example above.
bNum:=sqrt(bANumber);{Puts the square root of bANumber in bNum};
bNum:=discfree(2);{Puts how much disc space if free on drive 2 into bNum}

.... Remember: The above are NOT part of Pascalite

Functions don't have to "boil down" to numbers. "not" is a function, but it acts on a boolean value and returns a boolean value.
boAnswer:=not(boValue);
will make boAnswer hold "true" if boValue holds "false", and will make boAnswer hold "false" if boValue holds "true".

Function calls are sometimes written differently in deference to out earliest schooldays. It doesn't exist in any Pascal I know, but the following would be quite reasonable:
bNum:=Add(bFirst,bSecond);
It would "boil down" to the sum of bFirst and bSecond. The function is available in all Pascals, but it uses the other way of writing a function:
bNum:=bFirst + bSecond;
The clever bit isn't in using the "+" symbol in place of a word. The clever bit is that you are allowed to write out the function call the way the man in the street would do it.

There are several Boolean functions in Pascalite (and others) which use a similar syntax....

boAns:=boFirst and boSecond;{the way to write "and(boFirst, boSecond)"}
boAns:=boFirst xor boSecond;{the way to write "xor(boFirst, boSecond)"}
boAns:=boFirst or  boSecond;{the way to write  "or(boFirst, boSecond)"}
The first ("and") "boils down" to (i.e. "returns") true if boFirst AND boSecond hold "true". In deference to people who've become used to working with variables, I'll express the explanation of the next two rules the way they would be shown in most programming texts...
"boFirst or boSecond" returns true if boFirst OR boSecond is true.
"boFirst xor boSecond" returns true if boFirst OR boSecond is true, BUT NOT (eXcluding) if they are both true.

So! By now you have an idea of what a function is. Most Pascals, but not Pascalite, allow user defined functions. To make up a new function that would return 3 times the number you passed to it, you'd merely write....
function TripleIt(bNum:byte):byte;
begin
return:=bNum*3;
end;




Happily, although there are no user defined functions in Pascalite, you can achieve essentially the same result with user defined procedures. In the example just given, you would USE your "TripleIt" function as follows...
bAns:=TripleIt(bNum);


If you had a PROCEDURE with mere "value parameters", i.e. the first, limited, sort we used in a tutorial a while back, there'd be no sensible equivalent to the TripleIt function. However, the following procedure with its "variable parameters" WILL provide a TripleIt....
procedure(var bN:byte);
begin
bN:=bN*3;
end;
Now if we say....
TripleIt(bNum);{N.B.:There's no bAns:=}
... bNum will now be three times what it used to be. If we'd had a function to use, we could do bAns:=TripleIt(bNum); and get the answer into bAns without destroying the number stored in bNum. Without functions, you simply have to save things you will want again later before you mess them up.... bOldNum:=bNum; TripleIt(bNum);

It was only while writing this for you that I realise how few true functions there are in most Pascals and in Pascalite in particular. Many, many of the built in words that we use for getting things done, especially those which "boil down" to getting something done, or getting an answer, are procedures with value parameters. If you put the following line into a program, you first have to declare the variables bHr, bMin, bSec, bHun. Once you've done that, you can say....
GetTime(bHr, bMin, bSec, bHun);
... and the computer's current idea of the time of day (e.g. 4 hours, 27 minutes, 34 seconds and 15/100ths of a second past midnight) will be stored in the variables for you. This is a bit like a function call... as you pass GetTime, an answer is fetched for you. But it is not a function call because it doesn't "boil down" to that answer, it puts the answer into variables for you. Just to persue this distinction a little further: You MIGHT (though I haven't ever seen it!) have a FUNCTION called "GetHour". You might use it as follows.
bNum:=GetHour;

Done!
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