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