Prototypes local or global opinions wanted

Steve Summit scs at adam.mit.edu
Fri Jun 21 10:49:33 AEST 1991


In article <1991Jun20.202241.7531 at msuinfo.cl.msu.edu> draper at buster.cps.msu.edu (Patrick J Draper) writes:
>I'm interested in what others think of the practice of declaring
>function prototypes locally within a procedure.
>The advantage to the local declaration that I can see is that an
>unintentional call to bar() will be flagged by the compiler. I prefer to
>use the global declaration because if the definition of bar should
>change, there's one place to change the prototype.
>Any other opinions?

Most certainly.  If prototypes are used, they MUST be placed in
header files (which of course is usually tantamount to "global
placement"), and then #included by all source files in which the
function(s) is called, AND in the source file where the function
is defined.  This way, there's *really* only one place to change
the prototype, and the compiler can check the prototype against
the definition.

If a prototype for an external (i.e. defined in another source
file) function is placed in a source (non-header) file, it's far
too easy to forget to update it if/when the definition changes,
and the prototype then does more harm than if it hadn't been used
at all (it inserts implicit casts and/or generates warnings based
on an obsolete parameter list).

(This assumes you're using multiple source files at all, and is
written from a C perspective.  The use of prototypes is
considerably less optional in C++, and recommended practice for
prototype placement may be different.  My remarks are targeted at
comp.lang.c; note that this article, like the one to which it
responds, is also crossposted to comp.lang.c++ .)

The possibility of an inadvertent call (which might be flagged if
local prototypes were routinely used) is comparatively minor (and
one I've never worried about; I think that local extern data
declarations are a bit silly, too).

                                            Steve Summit
                                            scs at adam.mit.edu



More information about the Comp.lang.c mailing list