Sorting and summary of function related knowledge points in Lua

Time:2021-12-25

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

Lua language provides programs that can call a large number of built-in methods. For example, the method print () prints as input parameters in the console.

Functions are known names, such as methods or subroutines or programs.
Define a function

In Luaprograming languageThe definition of the method in the general form is as follows:

 

Copy codeThe 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 Lua programming language includes method header and method body. Here are all the parts of the method

  • Optional function scope: you can use the restriction function of keyword local scope or the ignored scope part, which will make it a global function.
  • Function name: This is the actual name of the function. The function name and the parameter list together form the functionautograph
  • Parameter: a parameter is like a placeholder. When a function is called, the value is passed to the parameter. This value is called the actual parameter or parameter. Parameter list refers to the type, order and number of method parameters. Parameter is optional; That is, the method may not have parameters.
  • Function body: the method body contains a collection of statements that define what the method does.
  • Return: in Lua, you can use the following comma separated return values, and the return keyword returns multiple values.

example:

Here is the source code of a function, calling max (). This function has two parameters num1 and num2 and returns the maximum value between them:

 

Copy codeThe 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 parameters:

If a function uses parameters, it must declare the accepted parameter value variables. These variables are called by the formal parameters of the function.

Formal parameters behave like other local variables inside the function and are created on entry and destroyed on exit.
Call function:

When creating a Lua function, you must define what kind of function to give. 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 called function defines the task and returns program control to the main program when executing its return statement or when the terminal of its function arrives.

The call is only a method that needs to pass the required parameters and method name. If the method returns a value, you can store the returned value. For example:

 

Copy codeThe 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 will get the following output.

 

Copy codeThe code is as follows:
The maximum of the two numbers is  10
The maximum of the two numbers is  6

 

Assignment and transfer functions

In Lua, we can specify the variables of a function or take them as parameters of another function. The following is a simple example to assign Lua to pass a function as an argument.

 

Copy codeThe code is as follows:
myprint = function(param)
   print(“This is 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 will get the following output.

 

Copy codeThe 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 its parameter. We can look at an example, using variable parameters, the function will return the average value.

 

Copy codeThe 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 will get the following output.

 

Copy codeThe code is as follows:
The average is 5.5