Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminology: Forward Declaration versus Function Prototype

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?

like image 806
Andrew Cottrell Avatar asked Dec 13 '11 21:12

Andrew Cottrell


People also ask

What is the difference between function declaration and function prototype?

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.

What is function declaration and prototype?

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.

What is forward declaration of a function?

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.

What's the difference between a function prototype and a header?

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.


2 Answers

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
like image 86
John Bode Avatar answered Oct 12 '22 01:10

John Bode


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.

like image 26
greydet Avatar answered Oct 12 '22 01:10

greydet