#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Nested functions


    can someone please explain what are nested functions exactly?
    I'm writing a program with some functions. some of those functions are used within other functions.
    I've done it before with another program that works fine but now I get this error:
    "ISO C forbids nested functions"
    can it be because I use a certain function within itself? I tried it with a little simple program just to check if it's possible and it worked fine. so I don't get what the problem is.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > "ISO C forbids nested functions"
    Like the message says, nested functions are not part of ISO C.

    GCC allows it (but then again, unconstrained GCC allows a lot of things).

    Nested functions look like this
    Code:
    void foo ( ) {
        int bar ( ) {
            return 42;
        }
        int res = bar();
    }
    In ISO C, do this
    Code:
    int bar ( ) {
        return 42;
    }
    void foo ( ) {
        int res = bar();
    }
    The only issue is the scope of bar, which in the former case is just the foo function, and in the latter, its the whole file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    thanx


    I actually did it correctly. just forgot a '}' in the end of main function. that's probably what caused the problem.

IMN logo majestic logo threadwatch logo seochat tools logo