Introduction to functions in Lua

Source: Internet
Author: User

In Lua, functions are the first type of values with specific lexical fields.

The first type of value indicates that the function in Lua has the same rights as other traditional types of values. Functions can be stored in variables, global variables, reject variables, or tables. They can be passed as real parameters to other functions and return values of other functions.

What does lexical domain mean? This means that a function can be nested in another function. Internal functions can access variables in external functions. Next we will see that this seemingly ordinary feature will bring great capabilities to the language. Because it allows the application of powerful programming techniques in various functional languages in Lua.

A confusing concept in Lua is that functions are anonymous like all other values, that is, they have no names. When discussing a function name, we are actually discussing a variable holding a function. This is similar to the fact that other variables hold various values and can operate on these variables in multiple ways.

A = {P = print}
A. P ("Hello World ")
Print = math. Sin
A. P (print (1 ))
Sin = A. P
Sin (10, 20)

If the function formula is worthwhile, can it be said that the function is created by some expressions? Yes, in fact, the most common method in Lua is function writing, for example:


Function Foo (X)
Return 2 * x
End

It is just a so-called syntactic sugar. That is to say, it is only a simplified form of the following code:


Foo = function (X)
Return 2 * x
End

 
Network = {
{Name = "grauna", IP = "210.26.30.34 "},
{Name = "Arraial", IP = "210.26.30.23 "},
{Name = "Lua", IP = "210.26.23.12 "},
{Name = "derain", IP = "210.26.23.20 "},
}
Table. Sort (Network, function (a, B) Return (A. Name> B. Name) end)

A function like sort accepts another function as a real parameter and calls it a high-order function. A high-order function is a powerful programming mechanism, applying anonymous functions to create real parameters required by higher-order functions can bring more flexibility. But remember, high-order functions do not have any privileges. Lua emphasizes that functions are regarded as the first type of values. Therefore, high-order functions are only an application embodiment of this viewpoint.

To further demonstrate the application of higher-order functions, a higher-order function about the derivative will be written. In an informal definition, the derivative of a function f at point X is (f (x + delta)-f (x)/Delta, where D tends to be infinitely small, you can use the following method to calculate the derivative of this function F:

Function derivative (F, Delta)
Delta = delta or 1e-4
Return function (X)
Return (f (x + delta)-f (x)/Delta
End
End
Derivative (f) is used to return the derivative of a specific function F, for example:


C = derivative (math. Sin)
Print (math. Cos (10), C (10 ))

Because a function is the first type of value in Lua, it can be stored in not only global variables, but also local variables and even table fields. As you can see, storing functions in table fields supports many advanced Lua applications. For example, modules and object-oriented programming!

 

 

Introduction to functions in Lua

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.