Re: Function definitions
Re: Function definitions
- Subject: Re: Function definitions
- From: Ondra Cada <email@hidden>
- Date: Sun, 9 Apr 2006 03:58:19 +0200
Greg,
On 9.4.2006, at 3:22, Greg Herlihy wrote:
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.
Nope, you are looking at it from the wrong perspective. The compiler
does *not* need (neither require) a prototype indeed. 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>
...is just a plain *programmer's fault*, for the proper code should
have looked this:
21 /tmp> >q.m
int sum(float a, float b) { return a+b; }
22 /tmp> >w.m
#import <Cocoa/Cocoa.h>
int main() { printf("wow! %d\n",sum(5.,5.)); return 0; }
23 /tmp> cc q.m w.m && ./a.out
wow! 10
24 /tmp>
Of course, since it is extremely easy to fall for this kind of
programmer's error, the compiler *allows* (!) you to use prototypes,
and if you do so, it (a) casts for you properly, (b) checks for your
typos (just as I've written previously, that were the first and
second main reasons to use headers).
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.
Just again you are taking it backwards -- here we get to the third
main reason for having headers: they are excellent and priceless as a
quick (and, properly commented, also as a thorough) API reference.
It's one of main Java lacks that it does *not* have headers, and thus
there is no nice and intuitive place to put API comments: sure, you
can intersperse them in implementation, but then you have either to
sift through all the code or to run something which does it for you
(in the case of Javadoc with very poor and counter-intuitive results)
before you can peep there for a quick ref how the API looks like. Not
speaking of the fact that the ugly Java thing, having no headers,
*bitterly* lacks the preprocessor, but that's another story.
To sum it all up:
(i) the C (and ObjC) compiler does not *need* function or message
prototypes (making so headers "superfluous" far as functions and
messages are concerned; you cannot do without them for global
variable references, macros, enums, typedefs, struct/union
declarations, inlines, and so forth);
(ii) though, the compiler *allows* us to use function and message
prototypes
- to be warned when we make a typo;
- to be relieved from the burden of casting properly manually;
and we would be fools if we did not exploit this enormous advantage;
(iii) headers, properly documented, also can serve as an *excellent*
API reference, which is just another reason to keep them and to write
them well.
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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