Lua function (1)

Source: Internet
Author: User
Tags unpack
Function purpose: 1. Complete the specified task. In this case, it is used as a call statement. 2. Calculate and return values. In this case, the function is used as the expression of the value assignment statement.
The parameter of the function is null. () must be used to call the function. Exception: When a function has only one parameter and the parameter is a string or table structure, () is optional. Lua provides the syntax O: Foo (x) for Object-Oriented function calls, which is equivalent to O. F (O, X.

The matching of the actual participating parameter of the Lua function is similar to that of the value assignment statement. The excess part is ignored and the NIL part is missing.
The Lua function can return multiple values. After return is used to list the list of values to be returned, the return value S, E = string. Find ("Hello Lua", "Lua ")

So how does Lua adjust the number of function return values to adapt to the call environment? 1. Use as statement, so the return value is ignored. 2. Use as expression
  • When used as the last parameter or only one parameter of the expression, the function returns as many values as possible based on the number of variables. If nil is not supplemented, it is exceeded.
  • Otherwise, the function call returns the first value (if no return value is nil)
  • When a function is called as a function parameter, it is assigned the same value as a multi-value.
  • When a function call is initialized in a table structure, it is assigned the same value as a multi-value.
  • Return F (); F () returns all values. You can use parentheses to forcibly return a value return (f ())
In C language, you can use a function pointer to call a variable function. You can declare a Variable Parameter Function, but not both. Lua can call the variable function of variable parameters like this: F (unpack (A) Unpack accepts an array as the input parameter and returns all elements of the array. Note that the number is returned. The ipair traversal result F = string. finda = {"Hello Lua", "Lua"} print (f (unpack (a) is returned )))
Variable parameters are represented by.... Lua places the variable parameters in the ARG table and contains a field where n represents the number of parameters. Function select (n,...) return Arg [N] End
Name function parameters: You can use a table as the unique parameter of a function to name parameters. Rename {old = "temp. lua ", new =" temp1.lua "} function Rename (ARG) OS. rename (Arg. old, Arg. new) end when there are many parameters or are easy to confuse, you can use the name parameter.
End

Lua function (1)

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.