Possible Duplicate:
function overloading in C
ANSI C doesn't permit function overloading (I don't sure about C99).
for example:
char max(char x, char y);
short max(short x, short y);
int max(int x, int y);
float max(float x, float y);
is not a valid ANSI C source code.
Which technique (or idea) should be used for function overloading problem in ANSI C?
Note:
An answer is renaming the functions, but which pattern should be used for renaming, that function names remain 'good function name'?
for example:
char max1(char x, char y);
short max2(short x, short y);
int max3(int x, int y);
float max4(float x, float y);
is not a good naming for max
function name.
Function overloading is a feature of Object Oriented programming languages like Java and C++. As we know, C is not an Object Oriented programming language. Therefore, C does not support function overloading.
This feature is present in most of the Object Oriented Languages such as C++ and Java. But C doesn't support this feature not because of OOP, but rather because the compiler doesn't support it (except you can use _Generic).
No, C does not support overloading, but it does support Variadic functions. printf is an example of Variadic functions.
In C++, two functions can have the same name if the number and/or type of arguments passed is different. Here, all 4 functions are overloaded functions. Here, both functions have the same name, the same type, and the same number of arguments. Hence, the compiler will throw an error.
Using the data type to be evaluated in the function name, for example
char max_char(char x, char y);
short max_short(short x, short y);
int max_int(int x, int y);
float max_float(float x, float y);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With