beginers of c said:
What's the actual use of declaring any function globally
for eg
#include<stdio.h>
int add() //function
int add(); /* a function declaration that is not also a
definition requires a terminating semicolon */
void main()
int main(void)
The reason for declaring a function is to give the compiler the information
it needs to do basic type-checking of the return value of that function.
If you make the declaration a full prototype:
int add(void);
or perhaps:
int add(int, int); /* returns the sum of two ints */
then the compiler can type-check the argument expressions as well.
The reason for doing this at file scope is that a declaration at block
scope only applies from the point of the declaration to the end of the
smallest enclosing block. Since functions are very often called from more
than one place, it is convenient to declare them at file scope so that you
only have to declare them once rather than in every function from which
they're called, and there is no real downside to doing things this way, so
you might as well take the win. (There *is* a downside to declaring
*objects* at file scope - if you want to know more about this, just ask.)
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999