I always thought that a function prototype must contain the parameters of the function and their names. However, I just tried this out:
int add(int,int);
int main()
{
std::cout << add(3,1) << std::endl;
}
int add(int x, int y)
{
return x + y;
}
And it worked! I even tried compiling with extreme over-caution:
g++ -W -Wall -Werror -pedantic test.cpp
And it still worked. So my question is, if you don't need parameter names in function prototypes, why is it so common to do so? Is there any purpose to this? Does it have something to do with the signature of the function?
int
parameters, without the names how does one know whether to pass inx
first ory
?add
function are not important, and most people already know that a function named "add" would add its parameters together without applying meaning to the values, so the names aren't really important in this example. But consider a function likestrstr
. The order is important. Which parameter comes first? Wouldn't it be easier to remember if the parameters had distinctive names, such asneedle
andhaystack
, and if your editor would show those names to you while you wrote your code or when it reported errors? That's readability.