Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Can a function call itself?

Asked by 7 years ago

I want to know if a function can call itself.. can it?

If so, how?

3 answers

Log in to vote
4
Answered by 7 years ago
Edited 6 years ago

Recursion


Yes, it's possible. What you're referring to is called recursion. If a function is calling itself, it's a recursive function. Most of the time, people opt for the use of recursion because it may be a shorter, more human-readable solution. However, that doesn't make it easier for the program.

Should I use recursion?

It depends. More often than not, you shouldn't need to have a function call itself. You can either have a separate function handle the task, or if you're trying to traverse a data structure (table, object, etc), you can use a loop.

Is recursion bad?

In short, and for most circumstances, yes. Recursion can be very costly. This of course depends on how deep the recursion is, but most of the time it exceeds practicality. Whenever the function calls itself again, it has to create a new address in memory to allocate all the local variables and instructions within that function. All of this data is stored in the stack, which is a special place in memory that stores information like local variables and return addresses. Eventually, however, the stack can get overwhelmed with addresses, and result in the infamous stack overflow error.

Conclusion

Try to avoid recursion. Loops are quicker, and more memory efficient. This was probably a lot of new information to take in at once, so if you have any questions, just let me know.

Disclaimer

Like in every situation, it depends on how it's implemented. Just because a function is calling itself, doesn't automatically mean it's inefficient. You want to avoid recurring function calls when they start to result in a tight loop. For example, this would be a poor implementation of recursion:

local function foo()
    foo() -- foo call itself, which calls foo, which calls itself...
end

foo() -- Call foo

Whereas this example is only calling itself once to toggle gate until it's false (this implementation is known as debouce).

local gate = false

local function foo()
    gate = not gate
    if gate then
        foo()
    end
end

foo()
Ad
Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

It can, but be carefull. if you dont add a return, or a wait (or any other form of yielding) it will lag out and crash.

The example below will find every instance inside o, and throw them into a table. Constantly using the same function again to find everything inside everything.

function GetDescendants(o)

    local AllObjects = {}

    function FindChildren(Object)
       for _,v in pairs(Object:GetChildren()) do
            table.insert(AllObjects,v)
            FindChildren(v)
        end
    end

    if o then 
        if typeof(o) == "Instance" then
            if o.Parent ~= nil then
                FindChildren(o)
            end
        end
    end

    return AllObjects
end
0
Would this example run over and over - forever? Because when it is called, it calls itself over and over, right? iiParadoxLegacy 38 — 7y
0
it will not RubenKan 3615 — 7y
0
it will not call itself infinitely, you will cause a stack overflow. When a function calls itself it is called recursion http://wiki.roblox.com/index.php?title=Recursion User#5423 17 — 7y
0
There's no point doing it like this anyways, and it will overflow regardless of whether there's a wait or not. foreverpower 80 — 7y
0
i talked to him in chat about what he needed, we discussed if, break, returns to cancel out the function. RubenKan 3615 — 7y
Log in to vote
-1
Answered by 7 years ago

Using something almost anyone hates: Recursion, however it can be quite useful sometimes. When I made an explorer I used recursive functions.

0
Speak for yourself. foreverpower 80 — 7y

Answer this question