• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Function definitions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Function definitions


  • Subject: Re: Function definitions
  • From: Greg Herlihy <email@hidden>
  • Date: Sat, 08 Apr 2006 18:22:14 -0700
  • Thread-topic: Function definitions

With Xcode's default settings, the compiler will compile a function call
even when no function prototype for the function being called is present.
But just because the compiler does not require a function prototype in this
situation does not mean that the compiler does not need a function
prototype.

Try this:

4 /tmp> >q.m
int sum(float a, float b) { return a+b; }
5 /tmp> >w.m
#import <Cocoa/Cocoa.h>
int main() { printf("wow! %d\n",sum(5,5)); return 0; }
6 /tmp> cc q.m w.m && ./a.out
wow! 8
7 /tmp>

So unless a programmer is diligent about providing function prototypes for
called functions, it is all too easy to wind up with a program like the one
above: one that compiles without any warnings - but when run - produces
completely the wrong results. The good news is that this problem can be
easily avoided. Just add:

    -Werror-implicit-function-declaration

to the "Other C Flags" setting in XCode's "Language" preferences. With the
implicit function declaration error enabled, any function call in a program
that is missing a function prototype is certain to cause a compile-time
error - instead of a runtime disaster.

To return to the original question regarding whether it is in fact necessary
to provide both function definitions in one file and matching function
declarations in another: the answer is that - were a C compiler like a Java
compiler, that is, were it able to derive a set of function declarations
from a set of function definitions - then the answer would be "no". It would
not be necessary for a programmer to provide a "header" file for a source
file - because the compiler could figure out on its own what such a header
file would contain.

Unfortunately, a C compiler does not produce header files, so the programmer
has to create them by hand. And creating a header file is sheer drudgery for
the programmer. After all, the programmer is doing nothing other than
providing the C compiler with the same set of information that the compiler
could - were it a bit smarter - be able to figure out on its own.

Greg


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Function definitions
      • From: Ondra Cada <email@hidden>
References: 
 >Re: Function definitions (From: Ondra Cada <email@hidden>)

  • Prev by Date: Re: Cocoa bindings performance
  • Next by Date: Re: Function definitions
  • Previous by thread: Re: Function definitions
  • Next by thread: Re: Function definitions
  • Index(es):
    • Date
    • Thread