function prototype in code block

Discussion in 'C Programming' started by anoncoholic, Apr 16, 2008.

  1. anoncoholic

    anoncoholic Guest

    Just found out that this is legal (news to me):

    int myfunc( int a )
    {
    int i = 0;
    i = i * a;
    int blah( float aa ); // <- huh?
    return i;
    }

    You can declare a function inside another function?? How long has this
    been possible? Note, i'm not talking about gcc's extension that supports
    nested functions. Just talking about the prototype.
     
    anoncoholic, Apr 16, 2008
    #1
    1. Advertising

  2. anoncoholic

    Guest

    anoncoholic <> wrote:
    > Just found out that this is legal (news to me):
    >
    > int myfunc( int a )
    > {
    > int i = 0;
    > i = i * a;
    > int blah( float aa ); // <- huh?
    > return i;
    > }
    >
    > You can declare a function inside another function?? How long has this
    > been possible?


    "Forever", although the ability to have a declaration after an executable
    statement as in the above example was new in C99. It would be clearer
    with an explicit "extern", but that's the default for functions, even
    inside a block.

    -Larry Jones

    Hey! What's the matter? Can't you take a joke?! It was a JOKE! -- Calvin
     
    , Apr 16, 2008
    #2
    1. Advertising

  3. anoncoholic wrote:
    > Just found out that this is legal (news to me):
    >
    > int myfunc( int a )
    > {
    > int i = 0;
    > i = i * a;
    > int blah( float aa ); // <- huh?
    > return i;
    > }
    >
    > You can declare a function inside another function?? How long has this
    > been possible? Note, i'm not talking about gcc's extension that supports
    > nested functions. Just talking about the prototype.


    Declare (as opposed to "define")? This has been possible... well,
    essentially always. Except that in C89/90 all declarations are supposed
    to be grouped at the very beginning of the block. Your code above is
    legal in C99, but not in C89/90 for that reason.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Apr 16, 2008
    #3
  4. anoncoholic

    Ian Collins Guest

    Eric Sosman wrote:
    > anoncoholic wrote:
    >> Just found out that this is legal (news to me):
    >>
    >> int myfunc( int a )
    >> {
    >> int i = 0;
    >> i = i * a;
    >> int blah( float aa ); // <- huh?
    >> return i;
    >> }
    >>
    >> You can declare a function inside another function?? How long has this
    >> been possible? Note, i'm not talking about gcc's extension that
    >> supports nested functions. Just talking about the prototype.

    >
    > Even so, my preference would be to declare logger() in
    > the same header file that defines LOGMESSAGE, and dispense
    > with the foolery. Maybe someone else can come up with a
    > more convincing use case.
    >

    One that springs to mind is proving a local wrapper for logger() by
    restricting the scope of the declaration, possibly adding specific
    content to the log messages.

    --
    Ian Collins.
     
    Ian Collins, Apr 16, 2008
    #4
  5. anoncoholic

    anoncoholic Guest

    Ian Collins wrote:
    > Eric Sosman wrote:
    >> anoncoholic wrote:
    >>> Just found out that this is legal (news to me):
    >>>
    >>> int myfunc( int a )
    >>> {
    >>> int i = 0;
    >>> i = i * a;
    >>> int blah( float aa ); // <- huh?
    >>> return i;
    >>> }
    >>>
    >>> You can declare a function inside another function?? How long has this
    >>> been possible? Note, i'm not talking about gcc's extension that
    >>> supports nested functions. Just talking about the prototype.

    >> Even so, my preference would be to declare logger() in
    >> the same header file that defines LOGMESSAGE, and dispense
    >> with the foolery. Maybe someone else can come up with a
    >> more convincing use case.
    >>

    > One that springs to mind is proving a local wrapper for logger() by
    > restricting the scope of the declaration, possibly adding specific
    > content to the log messages.
    >


    I see. Thanks for all the replies. I never knew that. It became an issue
    recently with some c++ code. I was trying to declare an instance of an
    object, on the stack, and call its default constructor. But the compiler
    kept acting weird because it thought I was declaring a function with no
    parameters that returns an object. Yep, I found the solution. Omit the
    parenthesis and the default ctor will be called.
     
    anoncoholic, Apr 17, 2008
    #5
  6. anoncoholic

    Ian Collins Guest

    CBFalconer wrote:
    > anoncoholic wrote:
    >> Ian Collins wrote:
    >>> Eric Sosman wrote:
    >>>> anoncoholic wrote:
    >>>>
    >>>>> Just found out that this is legal (news to me):
    >>>>>
    >>>>> int myfunc( int a ) {
    >>>>> int i = 0;
    >>>>> i = i * a;
    >>>>> int blah( float aa ); // <- huh?
    >>>>> return i;
    >>>>> }
    >>>>>
    >>>>> You can declare a function inside another function?? How long
    >>>>> has this been possible? Note, i'm not talking about gcc's
    >>>>> extension that supports nested functions. Just talking about
    >>>>> the prototype.
    >>>> Even so, my preference would be to declare logger() in
    >>>> the same header file that defines LOGMESSAGE, and dispense
    >>>> with the foolery. Maybe someone else can come up with a
    >>>> more convincing use case.
    >>>>
    >>> One that springs to mind is proving a local wrapper for logger()
    >>> by restricting the scope of the declaration, possibly adding
    >>> specific content to the log messages.

    >> I see. Thanks for all the replies. I never knew that. It became
    >> an issue recently with some c++ code. I was trying to declare an
    >> instance of an object, on the stack, and call its default
    >> constructor. But the compiler kept acting weird because it
    >> thought I was declaring a function with no parameters that
    >> returns an object. Yep, I found the solution. Omit the
    >> parenthesis and the default ctor will be called.

    >
    > You are thoroughly confused.


    Don't be an arse, I'm sure the OP knows the difference. The question
    was perfectly OK for this group.

    --
    Ian Collins.
     
    Ian Collins, Apr 17, 2008
    #6
  7. anoncoholic

    Richard Guest

    Ian Collins <> writes:

    > CBFalconer wrote:
    >> anoncoholic wrote:
    >>> Ian Collins wrote:
    >>>> Eric Sosman wrote:
    >>>>> anoncoholic wrote:
    >>>>>
    >>>>>> Just found out that this is legal (news to me):
    >>>>>>
    >>>>>> int myfunc( int a ) {
    >>>>>> int i = 0;
    >>>>>> i = i * a;
    >>>>>> int blah( float aa ); // <- huh?
    >>>>>> return i;
    >>>>>> }
    >>>>>>
    >>>>>> You can declare a function inside another function?? How long
    >>>>>> has this been possible? Note, i'm not talking about gcc's
    >>>>>> extension that supports nested functions. Just talking about
    >>>>>> the prototype.
    >>>>> Even so, my preference would be to declare logger() in
    >>>>> the same header file that defines LOGMESSAGE, and dispense
    >>>>> with the foolery. Maybe someone else can come up with a
    >>>>> more convincing use case.
    >>>>>
    >>>> One that springs to mind is proving a local wrapper for logger()
    >>>> by restricting the scope of the declaration, possibly adding
    >>>> specific content to the log messages.
    >>> I see. Thanks for all the replies. I never knew that. It became
    >>> an issue recently with some c++ code. I was trying to declare an
    >>> instance of an object, on the stack, and call its default
    >>> constructor. But the compiler kept acting weird because it
    >>> thought I was declaring a function with no parameters that
    >>> returns an object. Yep, I found the solution. Omit the
    >>> parenthesis and the default ctor will be called.

    >>
    >> You are thoroughly confused.

    >
    > Don't be an arse, I'm sure the OP knows the difference. The question
    > was perfectly OK for this group.


    You have the patience of Job. But well done for not allowing "Chuck" to
    continue his poison and incompetence out without reproach. He is a core
    cancer in this group.
     
    Richard, Apr 17, 2008
    #7
  8. anoncoholic

    anoncoholic Guest

    Ian Collins wrote:
    > Eric Sosman wrote:
    >> anoncoholic wrote:
    >>> Just found out that this is legal (news to me):
    >>>
    >>> int myfunc( int a )
    >>> {
    >>> int i = 0;
    >>> i = i * a;
    >>> int blah( float aa ); // <- huh?
    >>> return i;
    >>> }
    >>>
    >>> You can declare a function inside another function?? How long has this
    >>> been possible? Note, i'm not talking about gcc's extension that
    >>> supports nested functions. Just talking about the prototype.

    >> Even so, my preference would be to declare logger() in
    >> the same header file that defines LOGMESSAGE, and dispense
    >> with the foolery. Maybe someone else can come up with a
    >> more convincing use case.
    >>

    > One that springs to mind is proving a local wrapper for logger() by
    > restricting the scope of the declaration, possibly adding specific
    > content to the log messages.
    >


    I see. Thanks for all the replies. I never knew that. It became an issue
    recently with some c++ code. I was trying to declare an instance of an
    object, on the stack, and call its default constructor. But the compiler
    kept acting weird because it thought I was declaring a function with no
    parameters that returns an object. Yep, I found the solution. Omit the
    parenthesis and the default ctor will be called.
     
    anoncoholic, Apr 20, 2008
    #8
  9. anoncoholic

    anoncoholic Guest

    CBFalconer wrote:
    > anoncoholic wrote:
    >> Ian Collins wrote:
    >>

    > ... snip ...
    >>> One that springs to mind is proving a local wrapper for logger()
    >>> by restricting the scope of the declaration, possibly adding
    >>> specific content to the log messages.

    >> I see. Thanks for all the replies. I never knew that. It became
    >> an issue recently with some c++ code. I was trying to declare an
    >> instance of an object, on the stack, and call its default
    >> constructor. But the compiler kept acting weird because it
    >> thought I was declaring a function with no parameters that
    >> returns an object. Yep, I found the solution. Omit the
    >> parenthesis and the default ctor will be called.

    >
    > You are confused. C has no constructors. This is comp.lang.c. I
    > suspect you really want comp.lang.c++. C++ is an entirely
    > different language from C.
    >


    Yes, guys... I understand. Please lower the pitch forks. ;) I think you
    missed part of the thread. It was in fact a question about C.
     
    anoncoholic, Apr 21, 2008
    #9
    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. morrell
    Replies:
    1
    Views:
    992
    roy axenov
    Oct 10, 2006
  2. June Lee
    Replies:
    2
    Views:
    835
    Jim Cobban
    Apr 13, 2008
  3. Replies:
    9
    Views:
    224
    Thomas 'PointedEars' Lahn
    May 26, 2006
  4. Replies:
    3
    Views:
    283
  5. javascript fish
    Replies:
    0
    Views:
    189
    javascript fish
    Oct 11, 2008
Loading...

Share This Page