This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
SethShadowrider's profile picture

Vararg function

Started by SethShadowrider, 16 October 2012 - 07:32 AM
SethShadowrider #1
Posted 16 October 2012 - 09:32 AM
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.
remiX #2
Posted 16 October 2012 - 03:01 PM
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.
Ditto8353 #3
Posted 16 October 2012 - 03:32 PM
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 #4
Posted 16 October 2012 - 03:33 PM
… 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 #5
Posted 03 July 2013 - 12:25 AM
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!
LBPHacker #6
Posted 03 July 2013 - 06:46 AM
Quotation from above
Spoiler
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!
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 #7
Posted 04 July 2013 - 02:16 PM
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
LBPHacker #8
Posted 04 July 2013 - 02:46 PM
Quotation from above
Spoiler
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
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…)
TeamDman #9
Posted 04 July 2013 - 03:07 PM
Just making sure the OP understood fully for future use ;)/>
KaoS #10
Posted 04 July 2013 - 03:10 PM
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