Re: Ahead of "main"?



"Malcolm McLean" <regniztar@xxxxxxxxxxxxxx> writes:
"mdh" <mdeh@xxxxxxxxxxx> wrote in message
news:1177877117.874830.65670@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi all,
Going quite methodically through K& R ( as some of you can attest
to!), I have never seen a big diffference in declaring a function
within "main" or "ahead" of it. Now, (p119, K&R II), the discussion
states that "functions "whatever" " should be declared ahead of
main.
Is there a good reason for this?
thanks.

Some early versions of C had local functions, declared within the
function that called them. The idea never caught on, and it is now not
possible to declare functions within main.

I don't believe any early versions of C ever allowed you to *define*
functions within other functions (though some compilers allow it as an
extension).

It's always been possible to *declare* functions within a function
definition.

(A function definition includes the body that implements the
function. A definition is also a declaration.)

For example, the following is legal, and always has been, though
it's often considered poor style:

int outer()
{
int inner(); /* declare function inner */

inner(); /* call function inner, which must be
defined elsewhere */
}

The following is not legal (except as an extension) and never has been:

int outer()
{
int inner()
{
printf("Nope\n");
}

inner();
}

The real question is whether function *declarations* should appear at
file scope, or within a function definition. If a function is called
*only* from one function, it might make sense to declare it inside the
caller -- but since the definition has to be somewhere else anyway,
that doesn't really help encapsulation.

In any sizeable program, most of your functions are going to be in
separate source files, to be compiled and linked together with the one
containing main(). Usually you want function definitions in a .c
file, and declarations (to be visible to other translation units) in a
..h file.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: external variables
    ... Well, if you can't declare a function from within a function, what use is ... the keyword extern going to do you there? ... You can't *define* a function within a function definition: ... void outer{ ...
    (comp.lang.c)
  • Re: external variables
    ... Well, if you can't declare a function from within a function, what use is ... the keyword extern going to do you there? ... You can't *define* a function within a function definition: ... void outer{ ...
    (comp.lang.c)
  • Re: including files
    ... (I want to use some functions of the included file, which is not the "main" function, can I just declare the functions I want to use only at the header). ... void f; // declaration, no definition ... That way you can be sure that the prototype matches the actual function definition. ...
    (microsoft.public.vc.language)
  • Re: const and standard types
    ... > function definition you declare the value const, ... > void f(const int i) { ...
    (comp.lang.cpp)
  • Re: external variables
    ... inside of a function definition. ... Well, if you can't declare a function from within a function, what use is ... the keyword extern going to do you there? ... int main ...
    (comp.lang.c)