It's good practice to make local functions instead of global ones whenever you can. It'll help you avoid name collisions like these in the global table.
As with any local variable, you need to define the variable before you reference it elsewhere, even if you have to temporarily leave it
nil. Otherwise, Lua will try to look for it in the global table instead.
Option 1: Define the function before any reference you make to it.
Lua Code:
local function MyFunction()
-- Do stuff
end
-- Other functions that call MyFunction() should be placed after it
Option 2: Mark the function name as a local at the top of your Lua file.
Lua Code:
local MyFunction;-- Function "prototype"
-- You can place your functions in any order you want as long as they appear after the "local" statement above
function MyFunction()-- Lua will see this name is defined as a local at the top of the file and will place it in that scope instead of the global table
-- Do stuff
end
Option 2 is an easier way to convert existing code to use locals, but I consider
Option 1 to be cleaner and should be considered when writing new code.
PS: Keep in mind your addon shares the global table with all other addons too. It's suggested for every global you use (including frame and region names) are prefixed with the name of your addon to also help prevent name collisions.