3
function1()
    {
        statement1;
        statement2;
        function2()
        {
                statement3;
                statement3;
        }
    } 

why does control not enter function2, even though return type of both the functions are same

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
asir
  • 85
  • 2
  • 6
  • This doesn't even look like C! Where are the return types? – Nick Mar 15 '11 at 12:16
  • @Nick - depending on the dialect, a function with no return type defaults to `int`... but since it's a GCC extra, I wouldn't swear on it. – detly Mar 15 '11 at 12:33

3 Answers3

11

If you want to enter function2 you have to call it. The fact that you put it inside another function doesn't mean it's going to be executed, but yet declared and defined. You have to explicitly call it

function1()
    {
    statement1;
    statement2;
    function2()
    {
            statement3;
            statement3;
    }
 function2();

} 

And indeed Std C doesn't allow this. But it still depends on your compiler, so if you're doing this on some purpose, check with your compiler otherwise just pull the function2 declaration out of the function1's block

CoolStraw
  • 5,282
  • 8
  • 42
  • 64
  • I did by myself to gather a maximum of information in one answer. I was pointed to the update direction by Jon's answer. – CoolStraw Mar 15 '11 at 12:17
5

This is not legal C as defined by the standard. Does it even compile?

Update: Assuming GCC, CoolStraw's answer is correct.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • @jon it will compile but the control will not flow into inner function – asir Mar 15 '11 at 12:15
  • 1
    It's a [GCC extension](http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html). (And pseudo-code, missing the return types on both functions.) – Mat Mar 15 '11 at 12:16
  • GCC compiles it without warnings (after some usual changes like adding `main` and making the functions `void`). Also, `function2()` can be called inside `function1` like in CoolStraw's answer. – schnaader Mar 15 '11 at 12:17
  • 1
    @schnaader: gcc complains if you call it in standards compliant mode: `gcc -std=c99 -pedantic` **warning: ISO C forbids nested functions** – pmg Mar 15 '11 at 12:22
  • Who cares about GCC extensions? The OP didn't tag the question "GCC fluff", so this answer is correct. – Lundin Mar 15 '11 at 13:01
1

The answer to the original question is as CoolStraw said that it isn't enough to define a nested function, you have to explicitly call it at the point you want it executed. It must be decalred before that. It has nothing to do with whether it has the same type as the containing function.

As Mat said, defining a function inside a function is not allowed by the C standard. It is a gcc extension also supported by IBM's XLC compiler.

The result type of a function has defaulted to int since the original K&R C circa 1970, and is part of all C standards so far. This is not pseudocode or a gcc extra. It's generally discouraged now because it hides mistakes; for example, if you forget "#include " and use a string function returning a pointer, it will default to returning an int. If those types are the same size, you'll get away with it, but in 64 bit mode with 32 bit ints, discarding the upper half of a pointer and replacing it with the sign extension of the lower half is a nasty bug.

Many compilers can give some kind of message warning that you've omitted the type of a function, and many will also warn that you're calling a non-void function without using the result. Both are legal, both may be exactly what you intended, but both may be errors. And most compilers can also warn about non-standard compliant extensions like gcc's nested functions. If you care about portability, enable warnings like those.