The overlap between these three can be messy :-(
- functions which may call each other
- parameters and return values as the interface to functions
- local variables for mutable state inside a single function
- global variables for shared state between all functions
- first-class functions which may be used as parameters and values by other functions
closures to share state between certain functions
- with strict lexical scoping rules (to avoid combinatorial complexity of globals)
- immutable data structures, to minimize side effects and maximize concurrency
- chaining and iterators and collection pipelines
- instance variables (aka properties) as mutable state that survives a single function call
- methods to attach functions to an instance
- privacy (aka encapsulation) so the instance variables are shared only among the instance methods)
Three Styles, Summarized
|Style||Behavior||Local State||Shared State|
- Local state is the same for all styles! Parameters and local variables work the same no matter what.
- Methods and closures are each "function plus state"; they differ in where that extra state is stored:
- methods use state inside an object as properties
- closures use state in nested scopes of enclosing functions
The CPU Don't Care
All of these styles exist to make code more readable / maintainable / debuggable for humans...
...to the CPU, it's all just ones and zeros.