< String | UserGuide | Execution of scripts >


Functions

Functions are codeblocks that can be executed by calling the function at any point of execution.

Functions can take arguments and can return multiple values in lua.

Arguments

Given following function:
function foo (a,b,c)
  print (tostring(a),tostring(b),tostring(c))
end

This function declares three arguments that can be passed. If we call the function with less arguments than the defined ones, i.e. without any argument, the output will be

nil nil nil

Any missing argument is filled with nil. Calling functions without the correct number of arguments won't create an error during execution. However, it might throw an error once the values should be used.

For the same, calling the function with too many arguments won't throw an error either. If we call

foo(1,2,3,4,5)
The result will just be

1 2 3

All arguments that are passed too many will be discarded and are not available within that function.

Variable number of arguments

A function might receive a variable number of arguments. This means, that one might not know, how many variables will be passed. I.e. we'd like to have a function that prints out the types of the passed arguments. When we declare the function, we can use ... to define that from know on any variable that is passed will be stored in a table named arg which we can access then:

---% Code tested with Luxinia 0.96alpha build Dec 21 2006
---% at 12/29/06 13:43:39
-- function that take a variable number of arguments
function types(name,...)
  print(name)
  for i=1,arg.n do 
    print("  "..type(arg[i]))
  end
end

types("the types are",1,"bla",nil,{1,2,3})

Function environments

All functions have a so called environment which they operate in. We can set the environment of a function. This can be used to let a function have variables that look like global values or to control the variables a function can access, which is useful if code is executed that should not have access on certain functions:

---% Code tested with Luxinia 0.96alpha build Dec 21 2006
---% at 12/29/06 11:30:40
-- Function environment sample

str = [[
    for i=1,arg.n or #arg do 
        print(arg[i])
    end
]]

fn = assert(loadstring(str))

env = {}
setmetatable(env,
    {
        __index = _G,     -- delegate all
            -- reading access on global 
            -- variables to our global 
            -- environment
        __newindex = _G, -- delegate all
            -- writing access on global
            -- variables to our global
            -- environment
    })
setfenv(fn,env)
    -- set the environment to our function
    -- we can read and write now global
    -- variables from our function,
    -- however, we can manipulate some 
    -- variables by manipulating its 
    -- environment

env.arg = {1,2,3} -- let arg be 1,2,3
fn() -- will print 1 2 3

env.arg = {"hello","World"}
fn() -- will print hello World

rawset(env,"print",function () end)
  -- let's disable printing in our function
  -- we need to use rawset, otherwise the __newindex
  -- operator of the env table would overwrite
  -- our printing function in the global environment

fn() -- won't print anything

print "finished"