Ahead of "main"?

B

Barry Schwarz

Stephen Sprunk wrote, On 29/04/07 22:35:

Wrong. They can be *declared* within main or any other function, they
just cannot be *defined* in a function. A declaration says something
exists, a definition says what it is, the difference is important in C.


Actually, you should not put *all* your function definitions in header
files, since generally there are some which should be local to a given
source file and declared static in that source file. I.e. you should
always limit visibility to the smallest unit that makes sense, since
then you don't have to look as far to see all of the usage.

You should not put any function definitions in a header file. I would
go a step further and say no object definitions either. Only
declarations. The only definitions in a header file should be typedef
(oops, even though it is called a type definition in the standard, it
is specifically described as a declaration) and macro definitions.


Remove del for email
 
F

Flash Gordon

Barry Schwarz wrote, On 30/04/07 12:00:
You should not put any function definitions in a header file. I would
go a step further and say no object definitions either. Only
declarations. The only definitions in a header file should be typedef
(oops, even though it is called a type definition in the standard, it
is specifically described as a declaration) and macro definitions.

It was a trypo. I meant you should not put all your function
declarations in your headers.
 
G

Guest

Barry said:
You should not put any function definitions in a header file. I would
go a step further and say no object definitions either. Only
declarations. The only definitions in a header file should be typedef
(oops, even though it is called a type definition in the standard, it
is specifically described as a declaration)

All definitions are also declarations (macro definitions don't count
as "definitions"). A type definition is both a declaration and a
definition. A definition of an enumeration constant is also a
definition that can be appropriate for header files.
 
D

David Thompson

I'm not sure /exactly/ what you're saying; but if I read you
properly, what you're saying is false.

To the best of my knowledge, no "early" versions of C had local
functions, that is, functions /defined/ inside other functions.
(I don't count BCPL as an early version of C.)
And IIRC that vitiated the potential benefit by prohibiting access to
outerscope (nonglobal) variables, punting on the closure issue.
Again to the best of my knowledge, /all/ versions of C allow you
to /declare/ -- not define -- (external) functions inside functions.
Right.


Yes (where "Old" means "pre-Standard", for a useful value of "had").
I'm having trouble thinking of plausible nonuseful values of 'had'.
Whatever the order you used, the compiler "could" do such checking.
In practice it didn't: one used "lint".


Not true if by "prototype" you mean "declare with a prototype"
rather than "define using typed-argument syntax".
I'm not sure if your second alternative was meant to be "define using
prototype syntax" or "define using K&R1 syntax" or even both. But:
it is certainly not REQUIRED to use prototypes, even in C99, except
for variable arguments or arithmetic parameters narrower than int or
double. It is however always or almost so a good idea, and thus I
would agree with 'should'.
I'm not sure about that last: it's not /obvious/, even if it's
true.

Depends on who's looking. <G>

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
C

Chris Dollin

David said:
And IIRC that vitiated the potential benefit by prohibiting access to
outerscope (nonglobal) variables, punting on the closure issue.

While I'm all in favour of full lexical scoping, I don't think that's
compatible with the design of BCPL, and, for the same reasons, with
that of C.

One might be able to manage the downward case with a big dose of
"if the function escapes, The Behaviour Is Undefined". But even
then there's a Cost which people might not be prepared to pay
(since now the function [pointer] has to carry some kind of
scope reference around with it.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top