Why doesn’t C have nested functions?

Untitled Forums Programming Assignment Help Why doesn’t C have nested functions?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1089
    Aakanksha
    Participant

    Why doesn’t C have nested functions?

    #9653
    Aakanksha
    Participant

    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, though unsupported by qsort, is to pass around a pointer to a structure containing the necessary context.)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.