LUA does not support overloads like C, such as the following, which does not get the expected results in Lua
1 function Add (A, b) 2 return A + b3end45function Add (a,b,c) 6 return A + B + c7End
However, the function in Lua is very loose in the number of arguments passed in, and it automatically adapts to the parameters passed in, if the number of arguments passed in exceeds the number of definitions, The following parameters are automatically ignored, and if the number of parameters passed in is less than the number defined, the insufficient parameters will be nil. For example, the overloads you want to implement above can be done in the following form:
1 function Add (a,b,c) 2 if Nil Then 3 return A + b4 else5 return A + B + c< /c18>6 end7End
When calling, you can use Add, or you can use Add (1,2,3,4), but the final argument 4 is ignored, and the result is the same as Add.
Implementing a C-like function overload in Lua