A summary of function-related knowledge point collation in Lua

Source: Internet
Author: User
Tags lua

This article mainly introduces the function-related knowledge point collation in Lua, including the basic knowledge such as function parameter transfer and definition function, and the friends who need it can refer to the following

A function is a set of statements that perform a task together. You can put your code in a separate function. How to divide the differences between the functions of the code, but logically partitioning is usually to allow each function to perform a specific task.

The LUA language provides programs that can invoke a large number of built-in methods. For example, method print () is printed as an input pass parameter in the console.

A function is a known variety of names, such as a method or subroutine or a program.

Define a function

Methods in the LUA programming language are defined in general form as follows:

The code is as follows:

Optional_function_scope function function_name (argument1, argument2, Argument3 ..., ARGUMENTN)

Function_body

Return result_params_comma_separated

End

The method definition in the Lua programming language includes the method header and the method body. Here are all the parts of the method

Optional function scope: You can use the limit feature of the local scope of the keyword or the ignored scope part, which makes it a global function.

Function Name: This is the actual name of the function. The function name and the argument list together form the functional signature.

Parameter: A parameter is like a placeholder. When a function is called, the value is passed to the parameter. This value is referred to as the actual parameter or parameter. A parameter list is a parameter of a method of type, order, and quantity. The parameter is optional, that is, the method may have no parameters.

Function Body: The method Body contains a collection of statements that define what the method does.

Back: In Lua, the return keyword returns multiple values through the comma-delimited value returned by the following.

Example:

The following is the source code call MAX () for a function. This function has two parameters NUM1 and num2 and returns the maximum value between the two:

The code is as follows:

--[[function returning the max between two numbers--]]

function Max (NUM1, num2)

if (Num1 > Num2) Then

result = NUM1;

Else

result = num2;

End

return result;

End

Function arguments:

If a function uses arguments, it must declare an accepted parameter value variable. These variables are called by the form arguments of the function.

The formal parameters behave like other local variables within the function and are destroyed when the entry function is created and exited.

Call Function:

When creating a LUA function, give a definition of what functionality must be done. To use a method, you will have to call the function to perform the defined task.

When a program calls a function, the control of the program is transferred to the called function. The task that is defined by the called function, and the program control returns to the main program when the return statement that executes it or when the terminal of its function arrives.

The call is just a way to pass the required parameters and method names, and if the method returns a value, you can store the returned value. For example:

The code is as follows:

function Max (NUM1, num2)

if (Num1 > Num2) Then

result = NUM1;

Else

result = num2;

End

return result;

End

--Calling a function

Print ("The maximum of the two numbers is", Max (10,4))

Print ("The maximum of the two numbers is", Max (5,6))

When we run the above code, we get the following output.

The code is as follows:

The maximum of the two numbers is 10

The maximum of the two numbers is 6

assigning and passing functions

In Lua, we can specify variables for functions, or we can use them as arguments for another function. The following is a simple example of assigning Lua to pass a function as an argument.

The code is as follows:

Myprint = function (param)

Print ("This are my print function-# #", Param, "# #")

End

function Add (num1,num2,functionprint)

result = Num1 + num2

Functionprint (Result)

End

Myprint (10)

Add (2,5,myprint)

When we run the above code, we get the following output.

The code is as follows:

This is my print function-# 10 # #

This is my print function-# 7 # #

function and variable parameters

You can create a function with variable parameters in Lua ... As a parameter to it. We can look at an example by taking advantage of the variable argument that the function will return an average.

The code is as follows:

function Average (...)

result = 0

Local arg={...}

For i,v in Ipairs (ARG) do

result = result + V

End

return result/#arg

End

Print ("The average is", average (10,5,3,4,5,6))

When we run the above code, we get the following output.

The code is as follows:

The average is 5.5

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.