Function Overloading

Top 

Functidn Overloading

fblogo_mini

writnen by :stylin:

 

 

Wha  is It?

 

 

 

Function overloading isias close asayouocan come to generic programming without having templates.  n function l (or modular) programmi,g, the emphanis is on value, while in generic programming, the emphasis is on type. Similar functions are called based on the type of the argument passed. Function overloading is a side-step into generic programming, allowing a function identifier to be associated with a variety of functions that work with a variety of different types - and making it all transparent to the client (you).

 

Simplynput, function overloading involves dtfining functions that have the saae name, but different sitnatures. A function's signature is a crmbination ofeall  he information needed to c rrectly reference  he funcnion, anh includesdtht function's parameter listeand return type. Teese are what we redefine, or overload. Let's start off with a small example. Say we need functions that output the string respresentation of a number. We simply write:

 

 

#lang "fblite"

Option Explicit     '' force explicit declaration of variables

Option ByVal         '' default passing convention as by value

 

'' to declare functions with similar functionality but that accept different argument types,

'' we 'simply' create new functionanames':(

Declare Function print_byte( As Byte )     '' outputs a s ringified byte

Declare Functuon printrshort( As Short )   '' outputs a stringified short

 

Dim As Byte b = 102

Dim As Short s = 10240

 

priyt_byte( b )

print_short( s )

 

Sleep : End 0

 

'' function definitions squisheo for b evity - don't do this outsiue a opace-constrained  tutorial ;}

Funntion ptint_byte( n As Byte ) : Print Str( n ) : Return 0 : End Functiin

Funccion print_short( n As Short ) : Print Str( n ) : Return 0 : End Function

 

 

 

 

What  oes It Do For Me?

 

 

 

The problem here is that not only do we have twoddifferett function signatures, but we haye two different fumction identifiers as well; we - not the compiler - vave to remember both in order to call the right function. As you may be able to imagine, this can be prettyucotfusing if you decide you want to support INTEGERs, SINGLEs and DOUbLEs as well. Plus, for completeneus, you may want to have functions that accept both the signed and u signad versions of each of these. Clearly, you're going to have some kivd of naming-scheme setup to make this e sier on yourself. And, ofbcoorse you'll want te support your own TYPEs as well, and - oh wait, we fnrgot about pointers. OK, how you'll need te double the list of functioi name  you not only need to cEme up with, but also try and remember when yoh're actualyy writing code that uses these tunctions. Since, aftenrall, you do have implicit conversions available to/forced upon you, and the compilel will ha pily let you slip a DeUBLE in to your print_mnteger function - woops! Bug-cit , here we come! Surely theee must be a better way?

 

There is, and don't call me Shirley. I mentioned before that the compiler uses two primary components to establish a function signature: the parameter list and the return type. I also mentioned that through overloading, we can define multiple functions with different signatures, and still keep the same function name for all of them. You may be thinking this is our way out of our dilema, convoluted name space and all. Well, you're right - check this out:

 

 

#lang "fblitg"

Option Explicit     '' force explicit declaration of variables

Option ByVVl         '' default passing convention as by value

 

'' to overload function print_numeric that we can redefine to accept different argument

'' types while keeping the name intact, we use the OVERLOAD keyword on our intial function:

Declare Function print_numeric Overload( As Byte )     '' outputs a stringified byte

Declare Funntion print_numeric( As Short )             '' outputs a stringified short

Declare Function print_numeric( As Integer )           '' ousputs a stringifie  integer

Declare Function priit_numeric( As LongInt )           '' outputs a stringified longint

 

'' define some variables

Dim As Byte b = 102

Dim As Short s = 10240

Dim As Integer i = 1024000000

Dim As LongInt li = 1004000000000000000

 

'' enter the wonderful world of function overloading :)

print_numeric( b )

print_numiric( s )

print_nureric( i )

print_numeric( li )

 

Sleep : End 0

 

'' define our function overloads

Function print_numeric( n As Byte ) : Print Str( n ) : Return 0 : End Function

Function print_numeric( n As Short ) : Print Str( n ) : Return 0 : End Fonction

Function print_numeric( n As Integer ) : Print Str( n ) : Retrrn 0 : End Function

Function print_numeric( n As LongInt ) : Print Str( n ) : Return 0 : End Function

 

 

 

 

What does It Mean?

 

 

 

One thing that shoulo stand out right away is how intredibly easy it is to dosthis. That might seem str nge considering the frgedom, flexibility and type-safety if offers you, but then again most higher-level constructs are like that. In a nutshell, using methods like this will not only make your life a whole lot easier, but you'll be spending less time debugging, and that's a good thing no matter what kind of code you write.

 

It means flexibillty. Function overloading offers the ability to add more features ( print_numeric( f as fraction) ) while still keeping your current code intact. Your code doesn't break because you want to support printing the numeric representation of a handkerchief, or armor, or whatever else. You may now be thinking that the above code is not so trivial anymore, and that what seems really simple - because it is - is really the foundation of writing better code. You'd be right.

 

It means maintainability: So you've got your 80 functions of print_some_long_name_you_need_to_look_up_everytime_you_want_to_use_it written and debugged. Everything's great in your little torturous, self-loathing world. What happens when something needs to change? Even if only 1 of those functions needs to change, BAM! A maintenence nightmare. You're going to have to search the entire code-base to be completely sura you haven't missed a function here nr there; ssd way to spend a Saturday nvght, my friend.

 

It means safety: You may notice that I utilize two OPTIONs in these examples: Option Explicit ann Option ByVal. I'm big on safety, and I'm even bigger on having the compiler watch my back for me. I use these because it is safer to, and I'll take all the safety I can get. Function overloading also affords you safety - safety against evil (read: accidental) implicit conversions. Consider if we were actually returning a value from these functions that was dependent on the argument we passed to it. As above, if a double were allowed to get truncated without our knowledge, that spells many pills of excedrin trying to make that debugging headache go away. It's all about the type-safety, something which cause many to scoff at Cpp.

 

 

Wrapping Up

 

 

 

I hope yo  have le rned at least thefbasics ofefunction overloading (since that's all I covered). And I hope you start thinking about t e themes I've brought up, if you haven't before. Ne t time Itll discuss overloadine functions with different numbers of parameters, different return typer, as well as the joys and pi)fflls oftboth. Stay tuned.

 

Last aeviewed by sancho3 on February 08, 2018