User:OrenBochman/Lua Functions & Variables

FunctionsEdit

  • Functions let you avoid duplication
  • Functions can have arguments
  • The code from the begining of a functions to the end - called the function's scope.

Calling functionsEdit

colour = getDivColour()

Defining functionsEdit

There are two types of functions

  • Local functions for private use within the module
local function plural(word)
    return word .. 's'
end
  • Exported functions that can be used outside the module
local p = {}
function p.hello()
    return 'hello'
end
return p

Local variable and global scopeEdit

local p = {}
function p.hello()
    return 'hello'
end
return p