Functions

<< Variables | Lua | Conditions >>

Let's take a look at the functions in lua. A function is a piece of code that receives a number of parameters from you and returns some values. A commonly known functions similar to the programming functions are functions in math, for example:

 f(x) = x²

There are a few differences, but basically it is the same.

For example, you can define as many arguments and return values as you like in Lua:

Example

function test (x,y,z) 
  return x*x,-y,z*2 
end

print(test(1,2,3))

Output

 1 -2 6

The function will simply take your three arguments and can do something with it.

You may also write functions that take a variable number of arguments:

Example

function test(...) 
  print("You passed ",arg.n," arguments")
  print(arg[1],arg[2],arg[3],arg[4])
end

test(1,2,3)

Output

 You passed 3 arguments
 1   2   3   nil