Reputation: 932
function f(...)
return ...
end
And I call it like this:
f()
Example
a = f()
print(a) -- echoes 'nil', same as print(nil)
But
print(f()) -- echoes newline, same as print(), that is, no args
t = {f()} -- same as t = {}
So, what does f() return?
Update: did not know that functions can return 'void', found this http://lua-users.org/lists/lua-l/2011-09/msg00289.html meanwhile.
Upvotes: 2
Views: 2852
Reputation: 56714
The answer regarding the type could be the output of this command:
print(type(f()))
In this case, it prints:
bad argument #1 to 'type' (value expected)
So, a value is expected, but there is no value. => It returns nothing (void).
So, it's a normal behaviour to have: t = {f()}
<=> t = {}
Regarding the assignment, Lua assigns by default the nil
value if there is no value.
Upvotes: 3
Reputation: 932
I found that Lua function can return 'nothing', not even a nil. In this case, f()
returns nothing
. Using nothing
(without assignment) results in zero arguments in another function call(like print(f())
or in table constructor({f()}
).
print(a)
echoed nil because a
had no assigned value, print(any_name)
will echo nil aswell.
Upvotes: 2
Reputation: 1183
It returns all parameters you called it with.
f() -- has no parameter, returns nothing
If you do an assignment with less values than you have variables, i.e.
local a, b = 3
local c
Then that'll just end up with b and c being nil.
On the other hand, this would all do something:
f(1) -- returns 1
f(1, 2, 3) -- returns 1, 2 and 3
local t = {f(1, 2, 3)} -- table with the values 1, 2 and 3
Upvotes: 5