Possible Duplicate:
function overloading in C
Apologies if this is a dup but if it is, I can't find it.
In C, can you define multiple functions with the same function name, but with different parameters? I come from a C# background. In C#, the following code is completely legal.
//Our first function
int MyFunction() { //Code here return i; } int MyFunction(int passAParameter) { // Code using passAParameter return i; }
In my specific case, I would like to create a function that has one optional parameter (that is an int) at the end of the parameter list. Can this be done?
Function Signature A function's signature includes the function's name and the number, order and type of its formal parameters. Two overloaded functions must not have the same signature. The return value is not part of a function's signature.
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. However, we do have an alternative if at all we want to implement function overloading in C.
An overloaded function is really just a set of different functions that happen to have the same name. The determination of which function to use for a particular call is resolved at compile time. In Java, function overloading is also known as compile-time polymorphism and static polymorphism.
No. C does not support overloading.
No. In strict C, you cannot do overloading.
However, given that most C compilers also support C++, and C++ does support overloading, there's a good chance you can do overloading if you're using a mainstream C/C++ compiler.
But its not strictly standard or portable to pure-C environments.
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