←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Vararg function

SethShadowrider's Photo 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.
Quote

remiX's Photo 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)

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.
Quote

Ditto8353's Photo Ditto8353 16 Oct 2012

View PostsIdEkIcK_, 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.
Quote

faubiguy's Photo 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.
Quote

T. B. Jerremad's Photo T. B. Jerremad 02 Jul 2013

For any who stumble upon this again, I had this problem too.

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!
Quote

LBPHacker's Photo 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.
Quote

TeamDman's Photo TeamDman 04 Jul 2013

var = {...} takes n number of arguments and puts them into a table.
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)
end
script asd dsa
> You entered asd
> You entered dsa
Quote

LBPHacker's Photo 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...)
Quote

TeamDman's Photo TeamDman 04 Jul 2013

Just making sure the OP understood fully for future use ;)
Quote

KaoS's Photo 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
local function randomF(firstvar,...)
  local args={...}
end
to make args a table of all parameters passed to the function AFTER THE FIRST ONE

local function randomF(...)
  local args={...}
end
makes 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
Quote