Nested function in C

2019-01-02 18:18发布

Can we have a nested function in C? What is the use of nested functions? If they exist in C does their implementation differ from compiler to compiler?

Are nested functions allowed in any other language? If yes, then what is their significance?

8条回答
后来的你喜欢了谁
2楼-- · 2019-01-02 18:22

As others have answered, standard C does not support nested functions.

Nested functions are used in some languages to enclose multiple functions and variables into a container (the outer function) so that the individual functions (excluding the outer function) and variables are not seen from outside.

In C, this can be done by putting such functions in a separate source file. Define the main function as global and all the other functions and variables as static. Now only the main function is visible outside this module.

查看更多
无与为乐者.
3楼-- · 2019-01-02 18:24

No, they don't exist in C.

They are used in languages like Pascal for (at least) two reasons:

  1. They allow functional decomposition without polluting namespaces. You can define a single publically visible function that implements some complex logic by relying one or more nested functions to break up the problem into smaller logical pieces.
  2. They simplify parameter passing in some cases. A nested function has access to all the parameters and some or all of the variables in the scope of the outer function, so the outer function doesn't have to explicitly pass a pile of local state into the nested function.
查看更多
琉璃瓶的回忆
4楼-- · 2019-01-02 18:34

To answer your second question, there are languages that allow defining nested functions (a list can be found here: nested-functions-language-list-wikipedia).

In JavaScript, which is one of the most famous of those languages, some uses of nested functions (which are called closures) are:

  • To create class methods in constructors of objects.
  • To achieve the functionality of private class members along with setters and getters.
  • Not to pollute the global namespace (that goes for every language, of course).

to name a few...

查看更多
怪性笑人.
5楼-- · 2019-01-02 18:36

No you can't have a nested function in C. The closest you can come is to declare a function inside the definition of another function. The definition of that function has to appear outside of any other function body, though.

E.g.

void f(void)
{
    // Declare a function called g
    void g(void);

    // Call g
    g();
}

// Definition of g
void g(void)
{
}
查看更多
余欢
6楼-- · 2019-01-02 18:38

You cannot define a function within another function in standard C.

You can declare a function inside of a function, but it's not a nested function.

gcc has a language extension that allows nested functions. They are nonstandard, and as such are entirely compiler-dependent.

查看更多
路过你的时光
7楼-- · 2019-01-02 18:38

I mention this as many people coding in C are now using C++ compilers (such as Visual C++ and Keil uVision) to do it, so you may be able to make use of this...

Although not yet permitted in C, if you're using C++, you can achieve the same effect with the lambda functions introduced in C++11:

void f()
{
    auto g = [] () { /* Some functionality */ }

    g();
}
查看更多
登录 后发表回答