Jump to content




Vararg function


9 replies to this topic

#1 SethShadowrider

  • Members
  • 23 posts

Posted 16 October 2012 - 08: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.

#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 16 October 2012 - 02: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.

#3 Ditto8353

  • New Members
  • 138 posts

Posted 16 October 2012 - 02:32 PM

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.

#4 faubiguy

  • Members
  • 213 posts

Posted 16 October 2012 - 02: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.

#5 T. B. Jerremad

  • New Members
  • 1 posts

Posted 02 July 2013 - 11:25 PM

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!

#6 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 03 July 2013 - 05:46 AM

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.

#7 TeamDman

  • Members
  • 13 posts
  • LocationCanada

Posted 04 July 2013 - 01: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

#8 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 04 July 2013 - 01:46 PM

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

#9 TeamDman

  • Members
  • 13 posts
  • LocationCanada

Posted 04 July 2013 - 02:07 PM

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

#10 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 04 July 2013 - 02: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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users