To me these terms are essentially synonymous when using the C programming language. In practice I might prefer "forward declaration" for in-file prototypes versus "function prototype" for prototypes included via a header file. But even that is an artificial distinction when you consider what happens after preprocessing. Perhaps I'm missing something.
Is there a consensus for when to use one term versus the other?
A function declaration is any form of line declaring a function and ending with ; . A prototype is a function declaration where all types of the parameters are specified.
A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that the function may later be used in the program.
In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition.
The only difference between the function prototype and the function header is a semicolon (see diagram below). The function definition is placed AFTER the end of the int main(void) function. The function definition consists of the function header and its body.
The term "prototype" refers to a specific declaration syntax; specifically, that the number and types of parameters to the function appear in the declaration. Given a function definition of
int foo(int a, char *b) {...}
you can have any of the following declarations:
int foo(); // a declaration, but not a prototype
int foo(a, b); // a declaration, but not a prototype
int foo(int, char *); // a declaration and a prototype
int foo(int a, char *b); // a declaration and a prototype
IMO those are not really synonyms. To me "function prototype" refer to the function name and its parameters' and return's types. It does not only apply to what you call "forward declaration". All functions have a prototype.
We more often make a difference between a function declaration and its corresponding definition.
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