Logic Machine Forum
function with variable arguments - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: function with variable arguments (/showthread.php?tid=5753)



function with variable arguments - Andrea Becagli - 20.11.2024

I am currently trying to create a library for personal use to communicate in Modbus asci. I am trying to keep it as similar as possible to the luamodbus library built-in in the editor.
I wondered how the two write functions mb: write bits and mb: write registers have an indefinite number of arguments. 
While looking at the official Lua documentation, I found out I can use "..." as an argument in the function definition and then examine the arguments through the "arg" table. 

The problem is this seems not to be working while scripting on a Logic Machine which always gives back the same arg table.

Did I do something or wrong or maybe this is something that can not be used through normal scripting? Huh


RE: function with variable arguments - admin - 21.11.2024

Like this:
Code:
function fn(...)
  local args = {...}
  
  for i, v in ipairs(args) do
    log(i, v)
  end
end

fn('a', 'b', 'c')