d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Overloading Functions. > Is It Used Often Or Not Preferred?
Prev12
Add Reply New Topic New Poll
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Apr 17 2014 07:33pm
Quote (carteblanche @ 17 Apr 2014 19:58)
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);


I am aware of that but thanks for the good examples sir!
Member
Posts: 5,385
Joined: Mar 28 2014
Gold: 1,014.04
Apr 19 2014 01:23pm
Yep its overloadedd
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Apr 19 2014 02:53pm
Quote (BambinoLeBush @ 19 Apr 2014 14:23)
Yep its overloadedd


Lol
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Apr 19 2014 03:06pm
Quote (BambinoLeBush @ Apr 19 2014 11:23am)
Yep its overloadedd


Are you sure about that?
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Apr 19 2014 03:57pm
Quote (SelfTaught @ 19 Apr 2014 16:06)
Are you sure about that?


lol? :P nice cow.

This post was edited by NinjaSushi2 on Apr 19 2014 03:57pm
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Apr 19 2014 10:56pm
Quote (NinjaSushi2 @ Apr 19 2014 01:57pm)
lol?  :P nice cow.


^^

He's one badass cow

This post was edited by SelfTaught on Apr 19 2014 11:03pm
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll