lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Yes,

https://www.lua.org/manual/5.3/manual.html#3.4 last paragraph :

```
Both function calls and vararg expressions can result in multiple values. If a function call is used as a statement (see §3.3.6), then its return list is adjusted to zero elements, thus discarding all returned values. If an _expression_ is used as the last (or the only) element of a list of expressions, then no adjustment is made (unless the _expression_ is enclosed in parentheses). In all other contexts, Lua adjusts the result list to one element, either discarding all values except the first one or adding a single nil if there are no values.
```


On Thu, Jan 18, 2018 at 2:03 PM, Ivan <ivan.yanchin@gmail.com> wrote:
Hello!
I've found a strange behavior of a mutliret function if I call this function multiple times within a single statement.
This may be a known issue or an expected behavior, but I was not able to find anything about it.

I created a function that returns multiple values (I tested for two and three values), then I call them in a single statement and only the first returned value from the first function is included into the statement's results, while all values returned by the second call are included.

Here is an example:

```
function test()
   return 3, 2
end

print(test(), test())
```

The print function will receive only three argument, while I expect it to receive four. As a result, it prints this:
```
3 3 2
```

While I expect it to print "3 2 3 2". This happens for both Lua and C functions that return multiple results. If a statement returns only one call to a function and an additional value, then it also omits all values except the first:

```
print(test(), 3) -- > prints 3 3
```

If a statement has an explicit value on the first place, it handles all returned values:
```
print(3, test()) -- > prints 3 3 2
-- this also works fine:
print(3, 4, test()) -- > prints 3 4 3 2 
```

Is this an expected behavior or a bug? I used Lua 5.3.4 to test, it also works on the demo page at https://www.lua.org/cgi-bin/demo

Best regards,
Ivan.