1

can any one tell me can we declare a function in user defined function ? such as

int sum(int x, int y)
{
    int fun( float x);
}

Can we define a function inside a function ? As I know we can't define a function inside a function.

But I just did this and its work fine the code is given below :

{
    int main()
    { 
        func1();
        return 0;
    }
    func1()
    {
        int i = 0;
        auto func2()
        {
            i = 10;
            printf("Heloo i am func 2\n");
        }
        printf("Heloo i am func 1\n");
        func2();
    }
}

It works very fine .

Now can anyone tell me what is going around how a function inside a function is defined or working properly ?

Can anybody explain to me why the code is working ?

Now when I changed few lines of code it give me following problems The changes are following:

code:

{
    func1()
    {
        func2();
        int i = 0;
        auto func2()
        {
            i = 10;
            printf("Heloo i am func 2\n");
        }
        printf("Heloo i am func 1\n");
    }

error:

error: static declaration of ‘func2’ follows non-static declaration
note: previous implicit declaration of ‘func2’ was here

Now what are these error and why are they coming ?

If I call func2() in main function than it will show an error like undefined reference to func2

Now can anyone tell me what is wrong here ?

2
  • 2
    way too many question marks... Commented May 23, 2011 at 10:19
  • It should be noted that whether this is allowed or not is just a language nerd curiousity. In real-world applications, you will never write declarations nor definitions inside other functions. Commented May 23, 2011 at 11:23

5 Answers 5

2

The C standard allows the declaration of functions within functions (as in your first code snippet), but not the definition of functions within functions (although some compilers may offer it as a non-standard extension).

The same is true for C++, although newer versions (C++0x, etc.) allow you to define anonymous lambda functions. But this is something different.

Sign up to request clarification or add additional context in comments.

2 Comments

what is different i want to know that .why the code is executing ?
@user: Presumably you're building with a compiler that supports nested functions. And presumably it requires that the function is declared before it is invoked (this is certainly true for "standard" non-nested functions). This isn't the case in your code snippet that doesn't work.
1

gcc allows this in C only (not C++) via -fnested-functions, but this is of course non-standard and non-portable so you should probably not use nested functions unless you have a very good reason.

1 Comment

pal what about the error when i call the func2 before its definition
0

This, for foo,

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

is not a definition of foo. It is a declaration and it is perfectly valid (though strange: function declarations belong outside any other functions) in C.

What C does not allow, which is what you found out, is that definitions cannot be nested.

/* INAVLID EXAMPLE */
int main(void) {
    int foo(void) { return 0; }
}

Comments

0

When you call func2() before declaration, C assumes (according to standard) implicit declaration int func2(...), but when compiler finds func2() definition, it differs from previous implicit declaration, so compiler complains about it. If func2() precedes the call to func2(), it acts as both definition and declaration, so no problem there.

And of course func2() cannot be called from main() as its scope is limited to func1(), like you cannot access local variable from another function.

As others mentioned, nested functions aren't standard in C, although some compilers (like GCC) support this feature.

Comments

0

This is just a supplement to Paul R's answer. Assuming that your compiler is GCC, the end of the document in Paul's answer says:

A nested function always has no linkage. Declaring one with extern or static is erroneous. If you need to declare the nested function before its definition, use auto

So func2 has to be declared as auto before the call of it.
If you change the second code

func1()
{
    func2();
    int i = 0;
    ...

to

func1()
{
    auto func2(); /* declaration added */
    func2();
    int i = 0;
    ...

the code will be compiled.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.