Xref: utzoo comp.lang.c:40285 comp.lang.c++:14266 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!snorkelwacker.mit.edu!bloom-picayune.mit.edu!news From: scs@adam.mit.edu (Steve Summit) Newsgroups: comp.lang.c,comp.lang.c++ Subject: Re: Prototypes local or global opinions wanted Message-ID: <1991Jun21.004933.4197@athena.mit.edu> Date: 21 Jun 91 00:49:33 GMT References: <1991Jun20.202241.7531@msuinfo.cl.msu.edu> Sender: news@athena.mit.edu (News system) Reply-To: scs@adam.mit.edu Organization: Thermal Technologies, Cambridge, MA Lines: 38 In article <1991Jun20.202241.7531@msuinfo.cl.msu.edu> draper@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@adam.mit.edu