Vararg function
SethShadowrider 16 Oct 2012
Can anybody tell me what a vararg function is? I put
local tArgs = {...}
in my code and now I get an error message telling me I cant use '...' outside of a varag function when I try to run it.
local tArgs = {...}
in my code and now I get an error message telling me I cant use '...' outside of a varag function when I try to run it.
remiX 16 Oct 2012
What exactly does thee error message say?
using that allows you to set variables after the name of the program (not sure how to explain it)
If your program name is test and when you want to use it with set variables before opening it.
test lol derp hello
This would set inputOne to 'lol', inputTwo to 'derp' and inputThree to 'hello'.
Not sure if I'm even right but this is how I understand it to be.
using that allows you to set variables after the name of the program (not sure how to explain it)
local tArgs = {...} local inputOne = tArgs[1] local inputTwo = tArgs[2] local inputThree = tArgs[3]
If your program name is test and when you want to use it with set variables before opening it.
test lol derp hello
This would set inputOne to 'lol', inputTwo to 'derp' and inputThree to 'hello'.
Not sure if I'm even right but this is how I understand it to be.
Ditto8353 16 Oct 2012
sIdEkIcK_, on 16 October 2012 - 02:01 PM, said:
Not sure if I'm even right but this is how I understand it to be.
That's pretty much the just of it. VarArg means variable arguments (0 or more). You can't do much with the "variable" ... other than pull the arguments out of it. That is what <var> = {...} is for. It puts all of the arguments into a table so you can use the arguments as normal variables.
The article I linked outlines some of the errors with VarArgs and where/how they can be used. The main point being that they can be used at the beginning of your code outside of all functions, or in functions such as the following:
function ItsAFunction(...) print(arg[1]) print(arg[2]) --'And so on.' end
Using ... in a functions argument signature automatically declares the local 'arg' table.
faubiguy 16 Oct 2012
... contains all the variables passed to a function that is designed to take an unspecified number of variables. Programs in ComputerCraft are loaded like this, so ... contains all the arguments given to the program. You can put it in a table to access them later, as in local tArgs = {...}. The problem here is that ... is different for every function, so you'll have to put local tArgs = {...} in the main program body.
T. B. Jerremad 02 Jul 2013
For any who stumble upon this again, I had this problem too.
I solved it by moving
I hope that this helps!
I solved it by moving
args = {...}to the top of the file, then continued debugging, where I found I had missed various if/then's, for/do's, and end's. So the fact that Lua is throwing an error may mean that it thinks
args = {...}is inside a function you do not mean it to be in.
I hope that this helps!
LBPHacker 03 Jul 2013
Quotation from above
Spoiler
To be precise: Your entire program is a big function which gets declared by loadstring(). loadstring creates a vararg function, that's how you get the passed variables in ... (not a table, but a list of variables). This was when you use ... in the body of the program (inside the big function), you'll get the variables passed to the program, but inside a function (so not in the body) the ... means (or would mean) the variables passed to the function you are currently in, and if the function isn't a vararg function (has no ... in the declaration), you'll get the error you've been getting lately.
TeamDman 04 Jul 2013
var = {...} takes n number of arguments and puts them into a table.
Examples :
or
> You entered asd
> You entered dsa
Examples :
function print(...) local rtn = '' for _,v in pairs({...}) do rtn = rtn..tostring(v) end return rtn end print("asd"," dsa")>asd dsa
or
args = {...} for i,v in pairs(args) do print('You entered '..v) endscript asd dsa
> You entered asd
> You entered dsa
LBPHacker 04 Jul 2013
Quotation from above
Spoiler
And that's how to answer the question of the OP only partially and without bothering to read the other answers to check if the OP was answered or not. (I know, I know, desperate attempt to reach the 3-post-thresold...)
KaoS 04 Jul 2013
basically in a program
args={...}
sets args to be a table of all of the parameters given to the program
in a function you can say
basically as long as the function's params includes ... then it is a vararg function, if it does not have ... you may not include the ... expression within its code
args={...}
sets args to be a table of all of the parameters given to the program
in a function you can say
local function randomF(firstvar,...) local args={...} endto make args a table of all parameters passed to the function AFTER THE FIRST ONE
local function randomF(...) local args={...} endmakes args a table of all parameters passed to the function
basically as long as the function's params includes ... then it is a vararg function, if it does not have ... you may not include the ... expression within its code