The code itself works fine this is just a question on if nested functions are more taxing
Version A:
local LevelModule = {}
local maxLevel = 13
function LevelModule.addExp(level, exp, expToAdd)
exp = exp + expToAdd
local function xpNeeded(level) -- only used in the addExp function
return level * 70
end
local function recursion()
local neededExp = xpNeeded(level)
if level == maxLevel then
exp = neededExp
elseif exp >= neededExp then
exp = exp - neededExp
level = level + 1
recursion()
end
end
recursion()
return level, exp
end
return LevelModule
as you can see the xpNeeded function is nested inside the addExp function which makes everything more organized
Version B:
local LevelModule = {}
local maxLevel = 13
local function xpNeeded(level) -- only used in the addExp function
return level * 70
end
function LevelModule.addExp(level, exp, expToAdd)
exp = exp + expToAdd
local function recursion()
local neededExp = xpNeeded(level)
if level == maxLevel then
exp = neededExp
elseif exp >= neededExp then
exp = exp - neededExp
level = level + 1
recursion()
end
end
recursion()
return level, exp
end
return LevelModule
in this version the xpNeeded function is no longer nested, but it is still only used then the addExp function thus only defining it once but becomes less organized than version A
is defining a function more that once (having a nested function) costly, or is it worth nesting for the organization factor
TL:DR are nested functions worth using over non-nested functions for the sake of organization/are nested functions more taxing than non-nested functions
Note: this can all apply to the recursion function too