FANDOM


FunctionEdit

EssentialsEdit

A function is a very unique type of value. It is used to perform tasks. You must call a function using parenthesis in order to run the code within it.

ArgumentsEdit

An argument is a value that is inputted into the function call, then stored as a variable in the function's environment. Jump to samples.

Returning ValuesEdit

A function can also return values. Once you return a value, the function's execution is halted. You may return more than one value. Jump to samples.

MethodsEdit

Functions can be stored within tables. By doing so, you are able to call the function in a different way. Jump to samples.

SamplesEdit

ArgumentsEdit

Sample
The following is an example of function arguments.

Source:

local function foo(bar)
       print(bar)
end
foo('Foobar')
Output:

Foobar


Sample
The following is an example of multiple arguments.

Source:

local function foo(bar, baz)
       print(bar)
       print(baz)
end
foo('Foobar')
Output:

Foobar
nil


Sample
The following is an example of an arbitrary number of arguments.

Source:

local function foo(...)
       print(unpack(arg))
end
foo('Foo', 'Bar', 'Baz')
Output:

Foo       Bar       Baz


Returning ValuesEdit

Sample
The following is an example of returning one value.

Source:

local function foo()
       return 'Foobar'
end
print(foo())
Output:

Foobar


Sample
The following is an example of returning multiple values.

Source:

local function foo()
       return 'Foobar', 'Baz'
end
local a, b = foo()
print(a, b)
Output:

Foobar       Baz


MethodsEdit

Sample
The following is an example of calling a function in a table.

Source:

local table = {foo = function(a, b)
       return a[b]
end, bar = 'baz'}

print(table.foo(table, 'bar'))
Output:

baz


Sample
The following is an example of calling a method.

Source:

local table = {foo = function(a, b)
       return a[b]
end, bar = 'baz'}

print(table:foo('bar'))
Output:

baz


ExtraEdit

Call Syntactic SugarEdit

Sample
If the only argument to a function is a table, you can do this.

Source:

local function foo(bar)
       print(unpack(bar))
end

foo{'Hello', 'World'}
Output:

Hello       World


Sample
If the only argument to a function is a string, you can do this.

Source:

local function foo(bar)
       print(bar)
end

foo'Hello World'
Output:

Hello World


ClosuresEdit

Sample
The following is a closure in Lua.

Source:

local function foo()
       local i = 0
       return function()
              i = i + 1
              return i
       end
end

local a = foo()
print(a())
print(a())
local b = foo()
print(b())
print(a())
Output:

1 2 1 3



RecursionEdit

Sample
The following is an example of function recursion.

Source:

local i = 0
local function foo(bar)
       i = i + 1
       if i < 5 then
              print(i)
              foo()
       end
end

foo()
Output:

1 2 3 4


Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.