Safe to use Local table for Recursive function
Posted: Fri May 26, 2023 6:37 pm
As I tend to have a need for temporary tables inside my recursive path finding functions, I am therefore wondering could I just use Local table instead of making permanent table.
An example:
I didnt check this works, since this is anyway nonsensical example.
But point being that in this case I am using TempTable to check that same number is not being checked twice.
Making TempTable Global, will definetily work, but since this Table has no other use, but this temporary use here, then could I safely use Local here, since basically only reference to this Local table is being sent to Recursive function, so from that point of view it is safe, but is there a risk it would stop existing in middle of doing recursive functions, or a risk that some other table inside the recursive function would occupy its memory space?
An example:
Code: Select all
Function Recursive(n, round)
if Round>20 then return
If hasItem(TempTable, n) then return
TempTable[n] = true
Recursive(n+1, round+1)
Recursive(n*10, round+1)
endfunction
Local TempTable = {}
Recursive(1)But point being that in this case I am using TempTable to check that same number is not being checked twice.
Making TempTable Global, will definetily work, but since this Table has no other use, but this temporary use here, then could I safely use Local here, since basically only reference to this Local table is being sent to Recursive function, so from that point of view it is safe, but is there a risk it would stop existing in middle of doing recursive functions, or a risk that some other table inside the recursive function would occupy its memory space?