< Blocks | UserGuide | Operators >


A function is a piece of code that can be used later. Code means a description of what should be done. For example, a recipe that describes how to bake cookies is a function.

Functions can receive arguments - values which are associated with variables defined in the signature of a function. I.e.

function foo (x,y,z)

describes that a function is created that can take three arguments. The arguments are passed when the function is called:

foo (1,2,3)

so the variables x,y and z in the function itself are now associated with the values 1,2 and 3 - just as the function call told.

Functions can return values that can either be used or thrown away by the caller. Unlike in most programming languages, Lua is not limited to return a single value - instead, you can return as many values as you like:

function foo (x,y,z)
  return z,y,x


This creates a function, stored in the global variable named "foo". It takes three arguments and returns them in reverse order. Thus, the output will be:


Functions can - of course - call other functions. It can even call itself, creating a recursion.