Search code examples
closureslanguage-agnosticlanguage-design

Is it possible to have nested functions without closures?


My understanding of closures so far is that they combine "open" functions with their surrounding scope, essentially making them closed expressions.

I've seen several examples of how this is implemented in Javascript, most of them using nested functions to create an outer and inner scope and show how the language creates a "snapshot" of the surrounding scope for the inner function.

However, I know there's languages that don't implement closures, for example C. In this particular case, there's also no (native) support for nested functions, so it seems that it is not possible to replicate the "inner" and "outer" cases from JavaScript - and as far as I know, due to this, not having closures doesn't become a important problem.

However, would it be possible for a language to have nested functions and still have no closures? Or does one implies on the another?


Solution

  • The concept of a "closure" is only meaningful when a function can be executed from outside of the scope in which it's declared.

    In languages without first-class functions, i.e., where functions can't be passed around or assigned to variables, you could still have nested functions, but they couldn't be used as closures.