d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Overloading Functions. > Is It Used Often Or Not Preferred?
12Next
Add Reply New Topic New Poll
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Apr 16 2014 11:06pm
So I had a question regarding overloading functions. Is this method of programming used often or is it not preferred? Should the programmer use separate naming convictions for different functions?

Code
// Function prototypes
int square(int);
double square(double);


Code
int square(int number)
{
return number * number;
}


Code
double square(double number)
{
return number * number;
}


To me it seems very useful but at the same time I could see some people preferring not to use it. Is it harder to debug at all?


Thanks again y'all! :D
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 16 2014 11:23pm
There is nothing wrong with overloading functions. It is commonplace, and expected. Why waste time differentiating between

Code

int intSquare(int);
double dblSquare(double);


When you can easily get the differentiation from the parameters and return types. NOT overloading causes more confusion than overloading.
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Apr 16 2014 11:37pm
Minko pretty much covered it but if youre using c++ you could just use templates

Code
template<typename T>
T square(T number) {
return (number * number);
}


This post was edited by SelfTaught on Apr 16 2014 11:37pm
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Apr 17 2014 12:34am
Quote (SelfTaught @ 17 Apr 2014 00:37)
Minko pretty much covered it but if youre using c++ you could just use templates

Code
template<typename T>
T square(T number) {
    return (number * number);
}


That is new to me. I guess I haven't gotten that far yet.

Quote (Minkomonster @ 17 Apr 2014 00:23)
There is nothing wrong with overloading functions. It is commonplace, and expected. Why waste time differentiating between

Code
int intSquare(int);
double dblSquare(double);


When you can easily get the differentiation from the parameters and return types. NOT overloading causes more confusion than overloading.


Yeah Ducky told me the same thing more or less. Use'em if you got'em because it cleans up code.

He's on steam btw if anyone wants me to relay a message.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 17 2014 01:00am
Quote (NinjaSushi2 @ Apr 17 2014 01:34am)
That is new to me. I guess I haven't gotten that far yet.



Yeah Ducky told me the same thing more or less. Use'em if you got'em because it cleans up code.

He's on steam btw if anyone wants me to relay a message.


Tell him I think he is super cute, and to keep on being super cute.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 17 2014 04:14am
just to clarify, only overload (eg: same function name) if they perform similar tasks with different params. for example, if you have many functions named square that that multiply the parameter by itself, you don't want to have a function named square that draws an ascii square to cout. you'll confuse the hell out of everyone (including yourself).

This post was edited by carteblanche on Apr 17 2014 04:14am
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Apr 17 2014 02:12pm
Quote (carteblanche @ 17 Apr 2014 05:14)
just to clarify, only overload (eg: same function name) if they perform similar tasks with different params. for example, if you have many functions named square that that multiply the parameter by itself, you don't want to have a function named square that draws an ascii square to cout. you'll confuse the hell out of everyone (including yourself).


I understand that much. I'm going to start overloading as it is so much cleaner. Thanks God for function signatures.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 17 2014 05:08pm
Quote (NinjaSushi2 @ Apr 17 2014 04:12pm)
I understand that much. I'm going to start overloading as it is so much cleaner. Thanks God for function signatures.


some languages have default parameters so you dont need to make 4 different functions to do the same bloody thing
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Apr 17 2014 06:49pm
Quote (carteblanche @ 17 Apr 2014 18:08)
some languages have default parameters so you dont need to make 4 different functions to do the same bloody thing


??
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 17 2014 06:58pm
Quote (NinjaSushi2 @ Apr 17 2014 08:49pm)
??


suppose you have something like this:

Code
int parseInt(String value, int radix){
// parse the value into an int based on radix
}


when you wanna use the function, you have to do pass two arguments every time:
Code
parseInt("42", 10);


well guess what? 99% of the time, you wanna use radix = 10. so to simplify your code, you can do add a second function:
Code
int parseInt(String value, int radix){
// parse the value into an int based on radix
}
int parseInt(String value){
return parseInt(value, 10);
}


so now for your convenience, you can call:
Code
parseInt("42")


this is a classic example of overloading, essentially just to use a default value. now we have two functions. but some languages like C# have something called optional parameters. instead of writing two of these functions, you can write just one:

Code
int parseInt(int value, int radix = 10){
// parse stuff
}

and you can call it both ways:
Code
parseInt("42");
parseInt("42", 10);


This post was edited by carteblanche on Apr 17 2014 07:01pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll