Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #17370

Re: Function Prototypes: Necessary or just good practice?

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Function Prototypes: Necessary or just good practice?
Date 2012-02-15 08:31 -0500
Organization A noiseless patient Spider
Message-ID <jhgc40$9oi$1@dont-email.me> (permalink)
References <jhcq3f$sch$1@news.albasani.net> <20120214010408.773@kylheku.com> <jhfh4i$19v$1@news.albasani.net>

Show all headers | View raw


On 02/15/2012 12:51 AM, /dev/phaeton wrote:
> On 02/14/2012 03:09 AM, Kaz Kylheku wrote:
> 
>> With what warning options?
>>
>> How about with -Wall and -W?
> 
> Honestly?  None.  I suppose I should start using them.
> 
> 
> However, the gist that I'm getting from all this...
> 
> It's a good idea to do prototyping, and in the worst case scenario it is 
> just superfluous.

Keep in mind that your original question, whatever you might have
thought to the contrary, was not actually about whether prototyping
should be done, but about whether it should be done twice - once in a
function declaration, and a second time in the function definition. The
separate declaration often is, as you say, superfluous, and should not
be used if that is the case.

Whether you need to provide a separate declaration is just a matter of
how you organize your code, but you should always use a prototype
whenever you either declare or define a function. They're extremely
useful - they're never superfluous. That's primarily because they allow
the C implementation to recognize certain common kinds of mistakes, and
require it to give you a diagnostic message if you've made one of those
mistakes.

On the separate declaration issue:
The first part of a function's definition is, in itself, a declaration
of the function, so if you never use a function anywhere but in a single
file, and if you define that function prior to any calls to that
function, a separate declaration of the function unnecessarily
complicates your code, and should not be provided. The only case where
this doesn't work is mutually recursive functions - for instance, if
func1() calls func2(), and func2() also calls func1(), at least one of
them must be declared first, with the definition coming later.

For any function which is used in at least one file other than the one
in which it is defined, you should create a header file containing a
declaration of that function, and #include that header file in any file
where that function is used. You should also #include that header file
in the file where the function is defined - that way, if you made a
mistake and the declaration of the function isn't compatible with the
definition of the function, you'll get a diagnostic message telling you so.
-- 
James Kuyper

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Function Prototypes: Necessary or just good practice? /dev/phaeton <blahbleh666@no_more_spam.hotmail.com> - 2012-02-13 23:05 -0600
  Re: Function Prototypes: Necessary or just good practice? Johann Klammer <klammerj@NOSPAM.a1.net> - 2012-02-14 07:31 +0100
    Re: Function Prototypes: Necessary or just good practice? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-14 13:14 +0000
      Re: Function Prototypes: Necessary or just good practice? Ralf Damaschke <rwspam@gmx.de> - 2012-02-14 16:20 +0000
      Re: Function Prototypes: Necessary or just good practice? Peter Nilsson <airia@acay.com.au> - 2012-02-14 13:47 -0800
        Re: Function Prototypes: Necessary or just good practice? James Kuyper <jameskuyper@verizon.net> - 2012-02-14 17:01 -0500
          Re: Function Prototypes: Necessary or just good practice? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-14 22:35 +0000
            Re: Function Prototypes: Necessary or just good practice? James Kuyper <jameskuyper@verizon.net> - 2012-02-14 17:51 -0500
              Re: Function Prototypes: Necessary or just good practice? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-15 00:06 +0000
          Re: Function Prototypes: Necessary or just good practice? Peter Nilsson <airia@acay.com.au> - 2012-02-14 20:27 -0800
            Re: Function Prototypes: Necessary or just good practice? Keith Thompson <kst-u@mib.org> - 2012-02-14 22:24 -0800
  Re: Function Prototypes: Necessary or just good practice? Stephen Sprunk <stephen@sprunk.org> - 2012-02-14 01:18 -0600
  Re: Function Prototypes: Necessary or just good practice? Philip Lantz <prl@canterey.us> - 2012-02-14 00:05 -0800
  Re: Function Prototypes: Necessary or just good practice? Kaz Kylheku <kaz@kylheku.com> - 2012-02-14 09:09 +0000
    Re: Function Prototypes: Necessary or just good practice? /dev/phaeton <blahbleh666@no_more_spam.hotmail.com> - 2012-02-14 23:51 -0600
      Re: Function Prototypes: Necessary or just good practice? Kaz Kylheku <kaz@kylheku.com> - 2012-02-15 06:36 +0000
      Re: Function Prototypes: Necessary or just good practice? James Kuyper <jameskuyper@verizon.net> - 2012-02-15 08:31 -0500

csiph-web