开发者

Can a C function have multiple signatures? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

function overloading in 开发者_JAVA技巧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?


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.


No you must use a different name for each function (this is not true with C++, as it allows you to specify optional parameters)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜