Functions and parameters

I am very curious about the functions we use in Lua, and I don’t think I know how to use them as efficiently as I could, so I am asking if someone knows something more about functions and their parameters. I’ve noticed with some native functions that you can have multiple parameters–more than you would normally use–and I wonder how that works and how I! could do that with my functions.

First of all, I understand the simple method of transferring data in a table, so transferring lots of data as a single argument to a function is not new to me. But what I find is that there are functions, like math.max for instance, that you can input any amount of numbers, and it will return the maximum number out of the list. I can use the function math.max(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) and it will return 10. It is not limited to just two arguments, but when I make a function, I have to define every input with its own value. For example,
function Jmax(a, b, c, d, e, f, g, h, i, j) can take up to 10 arguments. Through programming genius, you can make it so that if there are less than 10 arguments, it knows what to do to compensate. But I can’t use this program with any more than 10 arguments! How do I eliminate that kind of threshold? How do you create a function in Lua that takes as many arguments as you want to send it? Writing out 250 possible arguments and programming for it has got to be the most ridiculous idea and the code for the program would almost certainly be too big to be practical. Does anyone know how the math.max function works or others like it (like the print statement), that take any amount of numbers as arguments? Is there any way to look up the code for library/native functions like math? [import]uid: 60706 topic_id: 11903 reply_id: 311903[/import]

this looks like what you’re looking for:

 function select (n, ...)  
 return arg[n]  
 end  

see: http://www.lua.org/pil/5.2.html

[import]uid: 70635 topic_id: 11903 reply_id: 43435[/import]

Thank you very much. I wasn’t looking for that function, but the link you gave was exactly what I was looking for. And also finding the Lua documentation is great, since I’ve had many other questions come up before. This will help tremendously. Thanks. [import]uid: 60706 topic_id: 11903 reply_id: 43569[/import]