Function Prototypes

In C++, function prototyping is mandatory, but it is optional in C. This difference shows up if you leave the parentheses empty when declaring a function. In C, empty parentheses mean you are foregoing prototyping, but in C++ they mean the function has no prototypes. That is, in C++, the prototype

int slice();

means the same as the following:

int slice(void);

For example, the following sequence is acceptable, if old-fashioned, C but an error in C++:

int slice();
int main()
{
...
   slice(20, 50);
...
}
int slice(int a, int b)
{
...
}

In C, the compiler assumes you used the older form for declaring functions. In C++, the compiler assumes that slice() is the same as slice(void) and that you failed to declare the slice(int, ...

Get C Primer Plus®, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.