A function is a group of statements that perform a task together, and you can separate your code into separate functions.
The Lua language provides many built-in methods that programs can call. For example, the method print() prints the parameters passed as input in the console.
define function
The general form of a method definition in Lua programming language is as follows −
optional_function_scope function function_name( argument1, argument2, argument3........, argumentn) function_body return result_params_comma_separated end
A method definition in the Lua programming language consists of a method header and a method body. Here are all the parts of the method -
Optional_function - optional, the default is global, you can use the keyword local to define as a local function.
function - This is the actual name of the function.
arguments − Arguments are like placeholders, when the function is called, a value is passed to the argument, this value is called actual parameter or argument.
function_body - The method body contains a collection of statements that define the method's function.
return - In Lua, multiple values can be returned by following the return keyword with comma-separated return values.
Following is the source code of the function named max() . This function takes two parameters num1 and num2 and returns the maximum value between the two parameters −
--[[ function returning the max between two numbers --]] function max(num1, num2) if (num1 > num2) then result = num1; else result = num2; end return result; end
Call functions
When creating a Lua function, you need to define the function's function. To use a method, you will have to call the function to perform the defined task.
To call a method, you just need to pass the required parameters along with the method name, and if the method returns a value, you can store the returned value. like-
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 Wu Ya Tutorial runs the above code, the following output will be obtained.
The maximum of the two numbers is 10 The maximum of the two numbers is 6
Transfer Function
In Lua, functions can be assigned to variables, and they can be passed as arguments to another function. This is a simple example of assigning and passing functions as arguments in Lua.
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 the above code is run, the following output will be obtained.
This is my print function - ## 10 ## This is my print function - ## 7 ##
function variable
A function can be created in Lua with variadic arguments using ... as an argument, in this example the function will return the mean and accepts variadic arguments.
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 Wu Ya Tutorial runs the above code, the following output will be obtained.
The average is 5.5