function code instances in Lua _lua

Source: Internet
Author: User

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:

f = Math.sin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.