First-Class Function
I don’t understand https://en.wikipedia.org/wiki/First-class_function
Why? For many programming tasks, it is useful to also allow functions to be values, so that they can be stored in variables and passed into and returned from procedures in the same way as other values. We call these first-class functions to emphasize that anything that can be done with any other value can also be done with a value that is a function.
In Scala, we can create a function value from a procedure as follows by writing just the name of the procedure:
Note: anonymous functions (lambdas) function values (first-class functions)
Although anonymous functions denote function values, just as the literal symbol 5 denotes the integer 5, so a language with anonymous functions must also support function values, function values can also be created from named procedures, so a language with function values need not support anonymous functions.
Anonymous functions are just a convenient alternate syntax for the same concept as named functions and do not in themselves introduce any interesting semantics.
Implementation
In a computer, we can use a function pointer to denote the address of the function body.
However, we also need to pas arguments.
To represent a function value, we additionally need a representation of an environment , a mapping from the free variables in the function body (such as increment) to their values (such as 1, 2, or 3). Thus, we will implement a function value using a Closure.
We allocate the parameters and frame of function values onto the Heap.
just to be safe, every procedure from which we create a function value (i.e., every procedure mentioned at a closure creation site) also has its frame and parameters allocated on the heap