Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are function prototypes necessary for C++?

Tags:

c++

prototype

Basically, I am reading through this book right here and in Section 1.6: Some Differences between C and C++ it is stated:

Another subtle difference between C and C++ is that in a C++ program, all functions must be prototyped.

I am sure that this is not true from all the C++ programs that I have written. Is this only true for some versions of C++? Is it also true for C?

like image 418
SDG Avatar asked Dec 08 '22 18:12

SDG


1 Answers

It has been true of C++ since the beginning (although in C++ it's just called a "declaration", not a "prototype").

As C existed decades ago, it allowed you to call a function without declaring it. You could, however, declare a function if you wanted to--usually to tell the compiler that it had a return type different from what the compiler would deduce on its own. So, in C a function declaration looks something like this:

long f();

Note that empty parens there. That's what separates a function "declaration" from a function "prototype" (though a prototype is basically a superset of a declaration, so a prototype also declares the function in question). A prototype always has something inside the parens to indicate the number and type of parameters the function accepts, on this general order:

short g(int a, double b);

If it doesn't accept any parameters, you have to put in void to indicate that:

int h(void);

If you leave the parens empty, that (as noted above) means it's a function declaration instead of a prototype--and that means you're telling the compiler the function's return type, but you're not telling it anything about the number or type of parameters.

C++ (since before it was called C++, if I recall correctly) has only had one concept instead of the two in C. In C++ every function must be declared--and a declaration always includes the number of parameters, and the type of each. This is absolutely necessary to support (for one obvious example) function overloading, where the correct function to call is determined from the number and types of arguments you pass in the call.

A function definition in C++ also acts as a function declaration. Every function must be declared, but the declaration doesn't have to be separate from the definition.

In reasonably modern C, you normally get pretty much the same--that is, a "new" (i.e., not ancient) type function definition also acts as a prototype for that function. As C was originally defined, it included a syntax for a function definition that looked like this:

int f(a, b, c) 
int a;
short b;
long c;
{
    // function body here
}

This defines the function, but the compiler treats it only as a function declaration, not a prototype--that is, it tells the compiler the return type, but the number and types of parameters (even though they're specified) are not used by the compiler in the same way they would be with a function prototype. C++ has never used or supported this style of function definition though.

like image 193
Jerry Coffin Avatar answered Dec 17 '22 18:12

Jerry Coffin