September 20, 2022


In Lua, functions are the main method to abstract statements and expressions. It can be used to deal with some special work, and can also be used to calculate some values.

Lua provides many built-in functions that you can easily call in the program. For example, the print () function can print the passed parameters on the console.

The Lua function serves two main purposes:
1. complete the specified task. In this case, the function is used as a call statement;
2. evaluate and return the value. In this case, the function is used as the expression of the assignment statement.

The following example defines the function max() with parameters num1 and num2. It is used to compare the size of two values and return the maximum value:

--[[function returns the maximum of two values --]]