Search code examples
c++functionfunction-declaration

The use of declaring function inside a function?


Possible Duplicate:
Is there a use for function declarations inside functions?

I know that inside function we can declare a function. What is the use of it? Can you please bring a simple example?


Solution

  • There is little value to declaring a function inside a function, unless you intend on defining it later and only having it available to that function- i.e., the function declaration is encapsulated.

    int main() {
        void foo();
        foo();
    }
    void some_other_func() {
        foo(); // ERROR
    }
    void foo() {
    }
    

    But that's it. In comparison to triggering the Most Vexing Parse, this is an extremely limited benefit at best.