Nested function in C

前端 未结 9 1593
眼角桃花
眼角桃花 2020-11-22 12:24

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?

9条回答
  •  醉酒成梦
    2020-11-22 12:58

    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 publicly visible function that implements some complex logic by relying one or more nested functions to break 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.

提交回复
热议问题