HOME - - - - - Lazarus Tutorials TOC - - - - - - Other material for programmers
StumbleUpon.ComRecommend to StumbleUpon

Overloading...

       (page ltN2-over.htm)

Use the same subroutine name for several things

"Overloading" is something allowed by certain programming languages. The language of the Arduino, and Lazarus, and Delphi, among others.

I don't think you "need" overloading, but you should understand it in case you come across it being used in a program you want to understand. And, of course, you might want to use it at some point.

What is "Overloading"... an example.

Suppose that the system you are programming has a screen on which you can draw lines. (Don't stop reading, Arduino programmers! For about $35 you can add a text a graphics and text display, driven by a single data line.)

And suppose there's a MoveTo(x,y) command which moves your "pen" to position x,y on the display, and a LineTo(x,y) which draws a line from where ever you may be to the x,y sent to it.

Right! That's all the "scene setting" we need.

Remember: We're on the way to seeing what "overloading" is.

With MoveTo and LineTo, we could build a subroutine to draw a triangle. It would have 6 parameters... the coordinates of the three corners of the triangle. We might invoke the subroutine thus....

DrawTriangle(10,10,50,10,50,20);

The definition might look something like the following. (This page is called both by my pages about Lazarus, and my pages about programming Arduinos. The following isn't EXACTLY right for either!)

DrawTriangle(x0,y0,x1,y1,x2,y2);
MoveTo(x0,y0);
LineTo(x1,y1);
LineTo(x2,y2);

And we could build a DrawRectangle subroutine...

DrawRectangle(15,15,55,15,55,20,15,20);

The definition might look something like the following...

DrawRectangle(x0,y0,x1,y1,x2,y2,x3,y3);
MoveTo(x0,y0);
LineTo(x1,y1);
LineTo(x2,y2);
LineTo(x3,y3);

(Yes... if the rectangle was always going to have two sides parallel to the x axis, we could be clever, use just 4 parameters. But forget that for this.)

So far, so simple, I hope you will agree. Don't worry because so far things are simple.

Using "overloading" in that context

Right! That's all we need to illustrate "overloading"

We have created two subroutines: DrawTriangle, and DrawRectangle.

We could use the name "DrawShape" for BOTH of them!

So how would the program know what to do when "DrawShape" is called?

By the NUMBER (and type) of the parameters.

There would be two "DrawShape" subroutines defined in the code. (Again: remember these aren't exactly right for Lazarus or Arduino.)

     DrawShape(x0,y0,x1,y1,x2,y2);
     //1 of 2 "DrawShape"s
     MoveTo(x0,y0);
     LineTo(x1,y1);
     LineTo(x2,y2);

... and...

     DrawRectangle(x0,y0,x1,y1,x2,y2,x3,y3);
     //2 of 2 "DrawShape"s
     MoveTo(x0,y0);
     LineTo(x1,y1);
     LineTo(x2,y2);
     LineTo(x3,y3);

If a call of DrawShape with 6 parameters was used, the program would "know" to use the first one. For a call or DrawShape with 8 parameters, the program would use the second one.

That's it! I've never felt the need of such a thing, but it is there. Maybe you think the "simple" environment where the subroutine is just called "DrawShape" without two names for two things that are both, at heart, "just" drawing a shape is tidier? People worked very hard to give us overloading. I'm probably missing something. I hope you "see" what I'm missing, and are very happy with "overloading".

And there's more....

You can even have two "different" subroutines using the same name when they both have the same number of parameters... as long as the data type of the parameters is different, as in...

void DrawShape(x0,y0,x1,y1,x2,y2:integer;)
//Draw big triangle
{
MoveTo(x0,y0);
LineTo(x1,y1);
LineTo(x2,y2);
}

void DrawShape(x0,y0,x1,y1,x2,y2:byte;)
//Draw small triangle
{
MoveTo(x0,y0);
LineTo(x1,y1);
LineTo(x2,y2);
}

Even though they both have the same number of parameters, if you call DrawShape with integer-type values, you'll use the first version; call it with 6 byte-type values, you'll use the second.

So now you know!

So. Clear? If that was helpful, Facebook "Likes", Google "Pluses" would be a welcome "thank you", and might help others find this. Sorry to beg, but it's a struggle to get these pages known.



   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search   BEWARE: There is stuff at my other two sites that the search above won't reveal. Go to either site (see links below) and use that site's FreeFind search button.

BEWARE: The search above only visits a selection of my stuff. Go to either of my other sites (see links below) and use that site's FreeFind search button, if you haven't found something you "know is there".


In addition to the tutorials for which this page serves as Table of Contents, I have other sites with material you might find useful.....

My other sites....
Sheepdog Software homepage.
My Arunet homepage.

... and some links to specific pages within them you might want....
You can't "play" all day... learn to use the Libre Office/ Open Office database. Free. Multi-platform.
The Arduino- LOTS of fun, for not much money. And beginner (intelligent beginner) friendly. And good pursuit for kids. Combine programming and electronics!
Designing printed circuit boards the KiCad way. Free. Multi-platform. Long established. PCB-fab houses take native KiCad files.
And lastly... Making maps... how we did it before GPS Indulge me? This discusses a worthwhile, fun (if presented intelligently) activity for kids, which can be undertaken on many levels... a simple 20 minutes, or weeks of engaging activity. (Also known to divert susceptible adults.)


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, please at least help me with Facebook links, etc (buttons at top of page). If you run an MS-DOS or Windows PC, please visit my freeware and shareware page, try something? Links on your page to this page would also be appreciated, of course!
Click here to visit editor's freeware, shareware page.


   Here is the way to contact the author of these Lazarus/Delphi tutorials, Tom Boyd.





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. (Mostly passes. A few problems caused by Google+ code.)

If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

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