In order to compile a function call, the compiler must first perform name lookup, which, for functions, may involve argument-dependent lookup(ADL). If these steps produce more than one candidate function, then overload resolution is performed to select the function that will actually be called. In general, the candidate function whose parameters match the arguments most closely is the one that is called.
As in your case:
1--> char* average(int i, float f, double d);
2--> double average(double d, float f, int i);
Here in both of your functions you have completly different argument list. 1st average is taking int
, float
, double
and the 2nd one is double
, float
, int
.
So, compiler decides which method to invoke first based in its arguments.
If you invoke method with argument (int, float, double)
the overloaded version takes 1st method and if you invoke method with argument (double, float, int)
the overloaded version takes 2nd method.
The choice if which overloaded method to call(in other words, the signature of the method) is decided at compile time. So, compiler will already know the signature of the method to be invoked.
While overloading methods we must keep the following rules in mind:
- Overloaded methods MUST change the argument list,
- Overloaded methods CAN change the return types.
- Overloaded methods CAN change the access modifier.
- A method can be overloaded in the same class or in a subclass.