Function definition nesting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • José de Paula

    #1

    Function definition nesting

    Does C99 support defining a function in the body of another function?
    I mean, something like:

    int a (void)
    {
    int x;
    int y;

    int b(int c)
    {
    /* function b definition here */
    };

    /* body of function a */
    }

    --
    Quidquid latine dictum sit altum viditur
  • Martin Ambuhl

    #2
    Re: Function definition nesting

    José de Paula wrote:
    [color=blue]
    > Does C99 support defining a function in the body of another function?[/color]

    No

    Comment

    • Malcolm

      #3
      Re: Function definition nesting


      "José de Paula" <jose_de_paula@ ig.com.br> wrote in message[color=blue]
      > Does C99 support defining a function in the body of another
      > function?
      >[/color]
      No. You can get most of the functionality of local functions by placing your
      function in a file of its own, and then declaring the sub-functions as
      static functions in the same file.


      Comment

      • Alexander Bartolich

        #4
        Re: Function definition nesting

        begin followup to Malcolm:[color=blue]
        > No. You can get most of the functionality of local functions by
        > placing your function in a file of its own, and then declaring
        > the sub-functions as static functions in the same file.[/color]

        No, that's not what nested functions are about.
        They have access to the local variables of their parent functions.

        procedure a;
        var x:integer;

        procedure b;
        begin
        writeln(x);
        end;

        begin
        x := 1; b;
        x := 2; b;
        end;

        Experienced programmers can write Fortran in any language.
        And any means.

        --
        Für Google, Tux und GPL!

        Comment

        • Tom St Denis

          #5
          Re: Function definition nesting

          Alexander Bartolich wrote:
          [color=blue]
          > begin followup to Malcolm:[color=green]
          >> No. You can get most of the functionality of local functions by
          >> placing your function in a file of its own, and then declaring
          >> the sub-functions as static functions in the same file.[/color]
          >
          > No, that's not what nested functions are about.
          > They have access to the local variables of their parent functions.[/color]

          so you make

          int test(void)
          {
          int a, b;

          int test2(void) { return a + b; }

          a = 4; b = 5;
          return test2() + 3;
          }

          can be turned into

          ---
          static int a, b;
          static int test2(void) { return a + b; }
          int test(void) { a = 4; b = 5; return test2() + 3; }
          ---

          Tom

          Comment

          • Keith Thompson

            #6
            Re: Function definition nesting

            Tom St Denis <tom@securescie nce.net> writes:[color=blue]
            > Alexander Bartolich wrote:
            >[color=green]
            > > begin followup to Malcolm:[color=darkred]
            > >> No. You can get most of the functionality of local functions by
            > >> placing your function in a file of its own, and then declaring
            > >> the sub-functions as static functions in the same file.[/color]
            > >
            > > No, that's not what nested functions are about.
            > > They have access to the local variables of their parent functions.[/color]
            >
            > so you make
            >
            > int test(void)
            > {
            > int a, b;
            >
            > int test2(void) { return a + b; }
            >
            > a = 4; b = 5;
            > return test2() + 3;
            > }
            >
            > can be turned into
            >
            > ---
            > static int a, b;
            > static int test2(void) { return a + b; }
            > int test(void) { a = 4; b = 5; return test2() + 3; }
            > ---[/color]

            That's not the same thing at all. In the nested-function version
            (which is not valid C), a and b are local variables, so each
            invocation of test() gets its own unique copy of each of them. In
            your static version, the lifetime of a and b is the entire execution
            of the program; recursive calls to test() will share the same copies.

            If your goal is to create a function that does the same thing as the
            original one, you might as well write

            int test(void)
            {
            return 12;
            }

            If you want to write something that duplicates the semantics of nested
            functions with access to the parent's local variables, it's possible
            (though probably not worth the effort), but making the local variables
            static isn't the way to do it.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            Schroedinger does Shakespeare: "To be *and* not to be"

            Comment

            • Tom St Denis

              #7
              Re: Function definition nesting

              Keith Thompson wrote:
              [color=blue]
              > Tom St Denis <tom@securescie nce.net> writes:[color=green]
              >> Alexander Bartolich wrote:
              >>[color=darkred]
              >> > begin followup to Malcolm:
              >> >> No. You can get most of the functionality of local functions by
              >> >> placing your function in a file of its own, and then declaring
              >> >> the sub-functions as static functions in the same file.
              >> >
              >> > No, that's not what nested functions are about.
              >> > They have access to the local variables of their parent functions.[/color]
              >>
              >> so you make
              >>
              >> int test(void)
              >> {
              >> int a, b;
              >>
              >> int test2(void) { return a + b; }
              >>
              >> a = 4; b = 5;
              >> return test2() + 3;
              >> }
              >>
              >> can be turned into
              >>
              >> ---
              >> static int a, b;
              >> static int test2(void) { return a + b; }
              >> int test(void) { a = 4; b = 5; return test2() + 3; }
              >> ---[/color]
              >
              > That's not the same thing at all. In the nested-function version
              > (which is not valid C), a and b are local variables, so each
              > invocation of test() gets its own unique copy of each of them.[/color]

              I'd say C doesn't know about "threads" so really the point is flexible.
              Given the only entry-point is test() the implementation is comparable.

              Tom

              Comment

              • CBFalconer

                #8
                Re: Function definition nesting

                Tom St Denis wrote:[color=blue]
                > Alexander Bartolich wrote:[color=green]
                > > begin followup to Malcolm:[/color]
                >[color=green][color=darkred]
                > >> No. You can get most of the functionality of local functions by
                > >> placing your function in a file of its own, and then declaring
                > >> the sub-functions as static functions in the same file.[/color]
                > >
                > > No, that's not what nested functions are about.
                > > They have access to the local variables of their parent functions.[/color]
                >
                > so you make
                >
                > int test(void)
                > {
                > int a, b;
                >
                > int test2(void) { return a + b; }
                >
                > a = 4; b = 5;
                > return test2() + 3;
                > }
                >
                > can be turned into
                >
                > ---
                > static int a, b;
                > static int test2(void) { return a + b; }
                > int test(void) { a = 4; b = 5; return test2() + 3; }
                > ---[/color]

                I'm glad you think so. You are wrong if you consider that a
                general transcription method. Consider recursive calls to test.

                --
                A: Because it fouls the order in which people normally read text.
                Q: Why is top-posting such a bad thing?
                A: Top-posting.
                Q: What is the most annoying thing on usenet and in e-mail?


                Comment

                • Mark McIntyre

                  #9
                  Re: Function definition nesting

                  On Mon, 05 Apr 2004 00:13:58 GMT, in comp.lang.c , Tom St Denis
                  <tom@securescie nce.net> wrote:
                  [color=blue]
                  >Keith Thompson wrote:[/color]
                  (re nested fn vs static fn)[color=blue][color=green]
                  >>
                  >> That's not the same thing at all. In the nested-function version
                  >> (which is not valid C), a and b are local variables, so each
                  >> invocation of test() gets its own unique copy of each of them.[/color]
                  >
                  >I'd say C doesn't know about "threads" so really the point is flexible.[/color]

                  Where did threads come from? If you call test() recursively, or even
                  iteratively, each iteration of the nested version gets a unique copy of the
                  locals, whereas the static version doesn't.

                  --
                  Mark McIntyre
                  CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                  CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>


                  ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
                  http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
                  ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

                  Comment

                  • Tom St Denis

                    #10
                    Re: Function definition nesting

                    CBFalconer wrote:
                    [color=blue]
                    > Tom St Denis wrote:[color=green]
                    >> Alexander Bartolich wrote:[color=darkred]
                    >> > begin followup to Malcolm:[/color]
                    >>[color=darkred]
                    >> >> No. You can get most of the functionality of local functions by
                    >> >> placing your function in a file of its own, and then declaring
                    >> >> the sub-functions as static functions in the same file.
                    >> >
                    >> > No, that's not what nested functions are about.
                    >> > They have access to the local variables of their parent functions.[/color]
                    >>
                    >> so you make
                    >>
                    >> int test(void)
                    >> {
                    >> int a, b;
                    >>
                    >> int test2(void) { return a + b; }
                    >>
                    >> a = 4; b = 5;
                    >> return test2() + 3;
                    >> }
                    >>
                    >> can be turned into
                    >>
                    >> ---
                    >> static int a, b;
                    >> static int test2(void) { return a + b; }
                    >> int test(void) { a = 4; b = 5; return test2() + 3; }
                    >> ---[/color]
                    >
                    > I'm glad you think so. You are wrong if you consider that a
                    > general transcription method. Consider recursive calls to test.[/color]

                    Meh... ok consider it a slightly similar implementation of it. Given that
                    the former is not valid C it's upto .... SCAPEGOAT.

                    Ah shut up y'all. I was just making talk.

                    Tom

                    Comment

                    Working...