Question 20.24

Why doesn't C have nested functions?


It's not trivial to implement nested functions such that they have the proper access to local variables in the containing function(s), so they were deliberately left out of C as a simplification. (gcc does allow them, as an extension.) For many potential uses of nested functions (e.g. qsort comparison functions), an adequate if slightly cumbersome solution is to use an adjacent function with static declaration, communicating if necessary via a few static variables. (A cleaner solution when such functions must communicate is to pass around a pointer to a structure containing the necessary context.)


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback