bazola
bazola

Reputation: 270

What can be passed to Lua functions as arguments?

I've been searching through the documentation and through various questions, but I have not seen a plain answer to this question. What types of values can be passed to Lua functions as arguments?

I believe the answer is:

What I am looking for is a comprehensive list of what all can be passed into a function.

Upvotes: 2

Views: 148

Answers (2)

Tom Blodget
Tom Blodget

Reputation: 20772

vararg expression as an argument

In addition to passing any value as an argument, there is also an argument that is not a value: ..., the "vararg expression."

It can only be used in the body of a function where it is a formal parameter. When used at the end of a list, it expands the list with all of the actual arguments it represents. When used before the end of a list, it expands the list only with the first actual argument it represents.

local function f(a, b, ...)
    print("f", a, b, ..., "end")
end

local function g(x, y, ...)
    print( "g", x, y, ...)
end

f()
f(1)
f(1,2)
f(1,2,3)
f(1,2,3,4)
f(1,2,3,4,5)
g()
g(1)
g(1,2)
g(1,2,3)
g(1,2,3,4)
g(1,2,3,4,5)

Upvotes: 1

lhf
lhf

Reputation: 72312

From the Lua reference manual:

All values in Lua are first-class values. This means that all values can be stored in variables, passed as arguments to other functions, and returned as results.

Upvotes: 4

Related Questions