Re: function prototype vs function declaration



"santosh" <santosh.k83@xxxxxxxxx> wrote in message
news:fk89fa$tmr$1@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Ravishankar S wrote:


"Richard Heathfield" <rjh@xxxxxxxxxxxxxxx> wrote in message
news:eNWdnUUdru5UPvranZ2dnUVZ8qXinZ2d@xxxxxxxxx
Ravishankar S said:

<snip>

It turns out that function
declarations and function prototypes are the same thing in standard
C.

No, it doesn't.

It is true that all function prototypes are function declarations,
but it is not true that all function declarations are function
prototypes.

<snip>

an example would help here.

int fx();

This is a function declaration, but not a prototype. In fact, argument
checking is turned of for functions declared like this. This is for
backwards compatibility and not recommended for new code. Use:

int fx(void);

or specify the parameters explicitly, as a prototype.

Thanks.

My amended article would be:

It turns out that there is no difference between function declarations and
function prototypes as far as modern use of C is concerned. When a function
is declared, the number and types of its arguments are also naturally
specified. Such a declaration is also a function prototype.

But it is also allowed to declare a function without specifying its
arguments. In this case the compiler turns off argument checking at the
point of its call. This is meant for backward compatibility (K&R C) should
not be used in normal code. For example:





int fx(); /* declaration , but not a function prototype */



int testFx(void)

{



/* Compiler will not check for argument type match */

if (fx(5) == 10)

{

return 11;



}

return 15;

}



/* Will not get the required arguments ! */

int fx(double a,double b,double c)

{

if((a + b) > c)

return 10;

else

return 11;

}



Thus it is easy for programmers to call the function with incorrect
parameters which needless to say has disastrous runtime consequences. ANSI C
mandates function prototypes as the method for function declaration.




.



Relevant Pages