Function Overloading

hi all
While learning function overloading i came across the following example to illustrate a correct and incorrect function overloading technique
1
2
float square(float f)
double square (float x);      // error 

corrected code is
1
2
float square(float f)  // different signature??? how come     
double square (double d);      // allowed???? 

please help me understand this.What is the logical explanation behind this.Why does compiler does not allow this....
thanks for your time,
cyber dude
The first snippet is not allowed because to overload a function the parameters must be different. Just changing the return type is not enough.

function signature consist from it name and number and types of arguments
1
2
void example(int x, int y = 0); //signature is example(int, int)
int example(int x, int y); //signature is example(int, int) : same as previous, not allowed 
1
2
3
4
void example(int x, int y = 0); //signature is example(int, int)
int example(int x); //signature is example(int) : not same as previous, allowed
//[...]
example(1);//however this will be ambiguous 
You can think of the examples this way: when you call the function and pass in a parameter of a specific type, how will the program know which function to call? The program must differentiate among functions with the same name based on which parameters each accepts.
Topic archived. No new replies allowed.