In Lua, a function is a "first class value" that has a specific lexical domain.
First Class value: Representation in Lua, functions have the same rights as other traditional types of values (numbers and strings), functions can be stored in a variable (whether global or local) or in a table, can be passed as arguments to other functions, and can be returned as values for other functions.
Lexical fields: Refers to a function that can be nested within another function. Internal functions can access variables in external functions.
See Example code:
Copy Code code as follows:
Todo
function foo (A, B, c)
Print (A, B, c);
End
Local c = foo;
C (2, 3, 4);
End
The output results are: 2, 3, 4
Example:
Copy Code code as follows:
Todo
function derivative (f, Delta)
Delta = Delta or 0.01
Print ("Delta:", Delta);
return function (x)
Return (f (x + Delta)-f (x))/Delta
End
End
Local c = derivative (Math.sin)
Print (Math.Cos), C (10))
End
In the example above,
Copy Code code as follows:
c = function (x) return (Math.sin (x + 0.1)-math.sin (x))/0.01
Because:
Copy Code code as follows:
Local c = derivative (Math.sin)
Will:
Copy Code code as follows: