Ahead of "main"?

Discussion in 'C Programming' started by mdh, Apr 29, 2007.

  1. mdh

    mdh Guest

    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.
     
    mdh, Apr 29, 2007
    #1
    1. Advertising

  2. "mdh" <> wrote in message
    news:...
    > 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.
    Old C also had no prototypes. So if you put functions in reverse order of
    hierarchy, the compiler could do additional checking of arguments. Nowadays
    we should prototype all functions, so it doesn't matter where main() is
    placed, though obviously it should be either the first or the last function
    for readbility.
    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Apr 29, 2007
    #2
    1. Advertising

  3. mdh

    Eric Sosman Guest

    mdh wrote:
    > 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?


    Yes. If you use a function before declaring it, the
    compiler makes assumptions about the arguments the function
    takes and the value it returns. If the assumptions don't
    match what the function actually does, you're in trouble.
    (The latest compilers try to keep you out of trouble by
    making no assumptions; instead, they issue error messages.)

    Here's a point to ponder: A function definition -- the
    type, name, arguments, and function body enclosed in { } --
    not only defines the function, but also declares it (two
    two-syllable verbs both beginning with DE, but they are not
    the same). It is possible to declare a function without
    defining it, by writing all the same things but omitting the
    function body and putting a ; where the { }-enclosed stuff
    would have been:

    double trouble(int fireBurn, int cauldronBubble);

    This tells the compiler about the arguments trouble() expects
    and the type of value it returns, which is enough to allow
    the compiler to invoke it properly (and to check for some
    errors, like writing only one argument). Anytime after a
    declaration line like this, you can call the function and
    all will be well.

    Still later, at some convenient point in the file, you
    can supply the actual definition of the function:

    double trouble(int fireBurn, int cauldronBubble)
    {
    if (fireBurn != 0)
    return (double)cauldronBubble / fireBurn;
    else
    return 42.0;
    }

    There are two main reasons (and some less pressing ones)
    for wanting to write declaration-only lines. First, as you
    move to larger programs you'll find yourself breaking them
    up into separately-compiled files: Why should you copy the
    entire body of trouble() into twenty different programs, when
    you can compile it once in one file and then let all twenty
    programs call it? To make this work, you need to write a
    declaration-only line for trouble() that the other twenty
    programs can use; the usual practice is to put this line in
    a .h file the twenty programs can all #include.

    A second reason pops up less frequently, but does occur
    now and then. What if you have two functions macduff() and
    macbeth(), and under some circumstances each of them calls
    the other?

    double macduff(int x) {
    ...
    if (! i_am_thane)
    y = macbeth(x);
    ...
    }

    double macbeth(int y) {
    ...
    layOn = macduff(y + 42);
    ...
    }

    No matter which definition you place first, the other function's
    definition will not have appeared by the time you get to its
    call. The compiler will either make a wrong assumption about
    the as-yet-undefined function, or will protest and sulk in a
    corner. The solution is to write a declaration-only line for
    one (or both) of the functions:

    double macbeth(int); /* optional: omit arg names */

    double macduff(int x) {
    ...
    }

    double macbeth(int y) {
    ...
    }

    --
    Eric Sosman
    lid
     
    Eric Sosman, Apr 29, 2007
    #3
  4. mdh

    mdh Guest

    On Apr 29, 1:41 pm, Eric Sosman <> wrote:
    > mdh wrote:
    > > Hi all,
    > > Going quite methodically through K& R ( as some of you can attest
    > > to!), I have never seen a ... diffference in declaring a function
    > > within "main" or "ahead" of it.


    >
    > Yes. If you use a function before declaring it,.....



    > Here's a point to ponder: A function definition --..... also declares it (two
    > two-syllable verbs both beginning with DE, but they are not
    > the same). It is possible to declare a function without
    > defining it,......


    Thank you....as I do C more and more, I realize that words really
    count.





    >
    > There are two main reasons (and some less pressing ones)
    > for wanting to write declaration-only lines. First, as you
    > move to larger programs you'll find yourself breaking them
    > up into separately-compiled files:




    >
    > A second reason pops up less frequently, but does occur
    > now and then. What if you have two functions macduff() and
    > macbeth(), and under some circumstances each of them calls
    > the other?
    >
    >




    Thank you Eric.
    So, if I understand you correctly, in my case for a small program, the
    key seems to be to understand that a declaration needs to occur prior
    to the function being called.
    Where that declaration actually occurs (prior to main, or within main)
    ( under these limited circumstances) is of no consequence. However,
    once one gets into the "real" world of C, this would be good
    programming ( as you noted with seperate .h files), so I may as well
    get used to practising good style!!
     
    mdh, Apr 29, 2007
    #4
  5. mdh

    mdh Guest

    On Apr 29, 1:40 pm, "Malcolm McLean" <> wrote:
    >
    > 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.
    > Old C also had no prototypes. So if you put functions in reverse order of
    > hierarchy, the compiler could do additional checking of arguments. Nowadays
    > we should prototype all functions, so it doesn't matter where main() is
    > placed, though obviously it should be either the first or the last function
    > for readbility.
    >



    tks.
     
    mdh, Apr 29, 2007
    #5
  6. "mdh" <> wrote in message
    news:...
    > 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?


    In Standard C, functions cannot be declared "within" main(); these are
    called nested functions and are implemented as an extension by some
    compilers, but in general you should never use them. Therefore, the debate
    is about whether to declare functions before or after main().

    As a rule, you should declare all functions before you call them; I won't go
    into the reasons why, as Eric did a good job of that. One style is to
    define all other functions before main(), which also declares them. The
    other is to declare all of your functions in a group near the beginning of
    the source, and then you can define them in any order you want. The latter
    style is effectively required when you move to multi-file projects, and the
    standard practice is to put all of your function declarations in header (.h)
    files so that each source file can simply #include the appropriate header
    files and then use whatever functions are needed.

    S

    --
    Stephen Sprunk "Those people who think they know everything
    CCIE #3723 are a great annoyance to those of us who do."
    K5SSS --Isaac Asimov


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Stephen Sprunk, Apr 29, 2007
    #6
  7. mdh

    Eric Sosman Guest

    mdh wrote:
    > On Apr 29, 1:41 pm, Eric Sosman <> wrote:
    >> [...]
    >> Here's a point to ponder: A function definition --..... also declares it (two
    >> two-syllable verbs both beginning with DE, but they are not
    >> the same). It is possible to declare a function without
    >> defining it,......

    > [...]
    > So, if I understand you correctly, in my case for a small program, the
    > key seems to be to understand that a declaration needs to occur prior
    > to the function being called.
    > Where that declaration actually occurs (prior to main, or within main)
    > ( under these limited circumstances) is of no consequence. However,
    > once one gets into the "real" world of C, this would be good
    > programming ( as you noted with seperate .h files), so I may as well
    > get used to practising good style!!


    I think you are still confusing "declaration" and
    "definition." You should declare every function before
    trying to call it (modern compilers change "should" to
    "must"), but you can define the function wherever you
    like. Since a definition is also a declaration (but not
    vice-versa), if you define the function before calling
    it you don't need a separate declaration. If you define
    the function after calling it, or if you define it in a
    separately-compiled file, you need a declaration prior
    to the first call.

    Eventually this will make sense. Trust me.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Apr 29, 2007
    #7
  8. On Sun, 29 Apr 2007 21:40:00 +0100, "Malcolm McLean"
    <> wrote:

    >
    >"mdh" <> wrote in message
    >news:...
    >> 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.


    It is certainly possible to declare a function within another.
    Defining is prohibited but declaring is allowed. The drawback is that
    the declaration has block scope and will not be visible to any other
    function in the translation unit.

    >Old C also had no prototypes. So if you put functions in reverse order of


    Not always possible if function1 and function2 call each other as part
    of a recursive algorithm.

    >hierarchy, the compiler could do additional checking of arguments. Nowadays
    >we should prototype all functions, so it doesn't matter where main() is
    >placed, though obviously it should be either the first or the last function
    >for readbility.



    Remove del for email
     
    Barry Schwarz, Apr 29, 2007
    #8
  9. mdh

    Flash Gordon Guest

    Stephen Sprunk wrote, On 29/04/07 22:35:
    > "mdh" <> wrote in message
    > news:...
    >> 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?

    >
    > In Standard C, functions cannot be declared "within" main(); these are
    > called nested functions and are implemented as an extension by some
    > compilers, but in general you should never use them.


    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.

    > Therefore, the
    > debate is about whether to declare functions before or after main().
    >
    > As a rule, you should declare all functions before you call them; I
    > won't go into the reasons why, as Eric did a good job of that. One
    > style is to define all other functions before main(), which also
    > declares them. The other is to declare all of your functions in a group
    > near the beginning of the source, and then you can define them in any
    > order you want. The latter style is effectively required when you move
    > to multi-file projects, and the standard practice is to put all of your
    > function declarations in header (.h) files so that each source file can
    > simply #include the appropriate header files and then use whatever
    > functions are needed.


    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.
    --
    Flash Gordon
     
    Flash Gordon, Apr 29, 2007
    #9
  10. In article <463503c7$0$16366$>,
    Stephen Sprunk <> wrote:

    >> 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?


    >In Standard C, functions cannot be declared "within" main(); these are
    >called nested functions and are implemented as an extension by some
    >compilers, but in general you should never use them. Therefore, the debate
    >is about whether to declare functions before or after main().


    This would be true if you said "defined" rather than "declared". It's
    perfectly legal to declare functions in main(), for example:

    int main(void)
    {
    int foo(void);
    ...

    and this does not create nested functions. It's not however
    a common style.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Apr 29, 2007
    #10
  11. On Apr 29, 1:40 pm, "Malcolm McLean" <> wrote:
    > "mdh" <> wrote in message
    >
    > news:...> 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.


    Nonsense. There is no problem declaring functions inside main or any
    other function. The reasons why you may not want to do so are the
    scoping rules, since the declaration would only be visible within the
    same block as the declaration. On the other hand, that's perhaps
    exactly why you would want to do it on the few occasions where it
    makes sense to do so.
     
    J. J. Farrell, Apr 29, 2007
    #11
  12. On Apr 29, 2:35 pm, "Stephen Sprunk" <> wrote:
    > "mdh" <> wrote in message
    >
    > news:...
    >
    > > 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?

    >
    > In Standard C, functions cannot be declared "within" main(); these are
    > called nested functions ...


    No, they're called function declarations are are certainly allowed
    within main() and any other function. You're confusing declaration
    with definition. You can't define a function within another function
    in Standard C.
     
    J. J. Farrell, Apr 30, 2007
    #12
  13. mdh

    Flash Gordon Guest

    Barry Schwarz wrote, On 29/04/07 23:29:
    > On Sun, 29 Apr 2007 21:40:00 +0100, "Malcolm McLean"
    > <> wrote:
    >
    >> "mdh" <> wrote in message
    >> news:...
    >>> 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.

    >
    > It is certainly possible to declare a function within another.
    > Defining is prohibited but declaring is allowed. The drawback is that
    > the declaration has block scope and will not be visible to any other
    > function in the translation unit.


    Of course, if Malcolm meant define, he was also wrong as far as K&R1 is
    concerned, since on page 3 it says, "Function definitions cannot be
    nested..."

    >> Old C also had no prototypes. So if you put functions in reverse order of

    >
    > Not always possible if function1 and function2 call each other as part
    > of a recursive algorithm.
    >
    >> hierarchy, the compiler could do additional checking of arguments. Nowadays
    >> we should prototype all functions, so it doesn't matter where main() is
    >> placed, though obviously it should be either the first or the last function
    >> for readbility.


    Pre-ANSI there was no requirement for the compiler to check the
    parameters even if the function was defined before use. Also on page 3
    of K&R1 it refers to using lint to detect inconsistent argument usage.
    --
    Flash Gordon
    Now with a near-mint condition copy of K&R1 courtesy of my company
    having a clear-out.
     
    Flash Gordon, Apr 30, 2007
    #13
  14. mdh

    mdh Guest

    Thank you all who answered. In fact, it does make a lot more sense now.
     
    mdh, Apr 30, 2007
    #14
  15. mdh

    mdh Guest

    On Apr 29, 2:42 pm, Eric Sosman <> wrote:
    >>

    > I think you are still confusing "declaration" and
    > "definition." You should declare every function before
    > trying to call it (modern compilers change "should" to
    > "must"), but you can define the function wherever you
    > like.



    Maybe I did not articulate that well, but I understand that, thanks.


    > Eventually this will make sense. Trust me.



    How could I not trust anyone who so beautifully intertweaves
    Shakespeare with Hitchhikers Guide to the Galaxy?
     
    mdh, Apr 30, 2007
    #15
  16. mdh

    Eric Sosman Guest

    [OT] Re: Ahead of "main"?

    mdh wrote:
    > On Apr 29, 2:42 pm, Eric Sosman <> wrote:
    >> I think you are still confusing "declaration" and
    >> "definition." You should declare every function before
    >> trying to call it (modern compilers change "should" to
    >> "must"), but you can define the function wherever you
    >> like.

    >
    >
    > Maybe I did not articulate that well, but I understand that, thanks.
    >
    >
    >> Eventually this will make sense. Trust me.

    >
    >
    > How could I not trust anyone who so beautifully intertweaves
    > Shakespeare with Hitchhikers Guide to the Galaxy?


    Be bloody, bold, and resolute, and never lose
    track of your towel!

    --
    Eric Sosman
    lid
     
    Eric Sosman, Apr 30, 2007
    #16
  17. "Malcolm McLean" <> writes:
    > "mdh" <> wrote in message
    > news:...
    >> 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) <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"
     
    Keith Thompson, Apr 30, 2007
    #17
  18. mdh

    mdh Guest

    On Apr 29, 7:05 pm, Keith Thompson <> wrote:

    > 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.



    tks.
     
    mdh, Apr 30, 2007
    #18
  19. Malcolm McLean said:

    > 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.


    If you could just sort of stop making stuff up, it would be helpful.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Apr 30, 2007
    #19
  20. mdh

    Chris Dollin Guest

    Malcolm McLean wrote:

    > 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'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.)

    Again to the best of my knowledge, /all/ versions of C allow you
    to /declare/ -- not define -- (external) functions inside functions.

    > Old C also had no prototypes.


    Yes (where "Old" means "pre-Standard", for a useful value of "had").

    > So if you put functions in reverse order of
    > hierarchy, the compiler could do additional checking of arguments.


    Whatever the order you used, the compiler "could" do such checking.
    In practice it didn't: one used "lint".

    > Nowadays we should prototype all functions,


    Not true if by "prototype" you mean "declare with a prototype"
    rather than "define using typed-argument syntax".

    > so it doesn't matter where
    > main() is placed, though obviously it should be either the first or the
    > last function for readbility.


    I'm not sure about that last: it's not /obvious/, even if it's
    true.

    --
    Prototypical Hedgehog
    Meaning precedes definition.
     
    Chris Dollin, Apr 30, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John M
    Replies:
    1
    Views:
    1,178
    Kumar Reddi
    May 29, 2005
  2. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  3. Frederick Ding

    int main() or int main(void)?

    Frederick Ding, Dec 3, 2005, in forum: C Programming
    Replies:
    10
    Views:
    653
  4. Ravi
    Replies:
    17
    Views:
    927
    Kenneth Brody
    Apr 1, 2006
  5. main() called inside main()

    , May 4, 2006, in forum: C Programming
    Replies:
    14
    Views:
    1,428
    Richard Heathfield
    May 7, 2006
Loading...

Share This Page