4

How is the function declaration different from the function prototype?

I'm posting this question in reference to this answer on a question.

Community
  • 1
  • 1
Bazooka
  • 1,428
  • 4
  • 15
  • 24

4 Answers4

8

A Function declaration may/may not not include the function arguments.
While a Function Prototype must include the function arguments.

From Wikipedia:
Consider the following function prototype:

int fac(int n);

This prototype specifies that in this program, there is a function named fac which takes a single integer argument n and returns an integer. Elsewhere in the program a function definition must be provided if one wishes to use this function.

It's important to be aware that a declaration of a function does not need to include a prototype. The following is a prototype-less function declaration, which just declares the function name and its return type, but doesn't tell what parameter types the definition expects.

int fac();
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • but on the link that i posted along with the question it explicitly says otherwise.Look at the very last line on the answer. – Bazooka Jan 27 '12 at 15:22
  • 1
    It's worth a mention that `int fac();` is a non-prototype, while `int fac(void);` is a prototype for a no-argument function. – dbkk Jun 05 '17 at 19:34
4

A prototype is a declaration, but a declaration not always is a prototype. If you don't specify the parameters, then that's only a declaration and not a prototype. That means the compiler won't reject a call to that function complaining it wasn't declared, but won't be able to check if the parameters passed are correct (as it would if you had a prototype).

Fabio Ceconello
  • 15,819
  • 5
  • 38
  • 51
1

A function prototype is a function declaration that specifies the number and types of parameters.

T foo();               // non-prototype declaration
T foo(int, char *);    // prototype declaration
T foo(int a, char *b); // prototype declaration
John Bode
  • 119,563
  • 19
  • 122
  • 198
0

The prototype tells the compiler hey there is a function that looks like this and this is its name int getanint(). When you use that function the compiler puts the code calls that function and leaves a place to insert the address to the code that defines what that function does.

So in File Header A;

int getanint();

IN main.c

int main(...)
{ 
    getanint();
}

when you compile main.c it has no idea what getanint does or even where it is the created .o file is incomplete and is not enough to create an actually program. remember that the compiler operates on a single file that file can be very large because of the #include directives but they create a single file.

When you complie A.cpp

int getanint()
{   
    return 4; 
}

You now have the code for getanint in an object file.

To actually make a program you have to get main.o and A.o together and have the definitions of the functions inserted into the appropriate places. That is the job of the linker.

rerun
  • 25,014
  • 6
  • 48
  • 78