Table as parameter in lua

I want to have a table named contents as a parameter of a function named func.
So when I call this function I can input strings into the table at the instance the function is called as follows:

function func(contents)
  contents = {}
  print(table.concat(contents, ' '))


func({'content1', 'content2', 'content3'})

NOTE : the table should be created inside the function’s parameter

>Solution :

What you are asking for is the same as simply using table.concat() directly itself, there is no need to define a function for it unless you want to add additional manipulation.

function func(contents)
    return table.concat(contents, " ")

func({"content1", "content2", "content3"}) -- This returns 'content1 content2 content3'.

The above is the same as:

table.concat({"content1", "content2", "content3"}, " ") -- Returns 'content1 content2 content3'.

Leave a Reply