Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

The function won't return anything?

This is my first time learn about recursive function, and I wrote a test code:

local function func(a)
    a = a + 1

    if a == 100 then
        return a
    elseif a > 100 then
        return 100
    else 
        func(a)
    end
end

print(func(0)) --return nothing

The weird thing is it return nothing. Nothing in the output, not nil or anything, just nothing.

Here is another script that do the same thing, but it work:

local function func(a)
    for i = 1, 100 do
        a = a + 1
        
        if a == 100 and not a > 100 then
            return a 
        else
            return 100
        end
    end
end

print(func(0)) --return 100

Did I do anything wrong or I'm just retarded.

Thank for reading :D

1 answer

✓ Accepted answer

You forgot to add a return, line 09 should be

return func(a)

4 comments

0

Can you explain more please.

1

If you put a return there, calling func(0) will return func(1). But func(1) returns func(2), and func(2) returns func(3) and so on until it gets to func(100) which just returns 100. So the whole thing returns 100.

0

So what happen if I'm not put a return there?

0

I mean yeah it won't print anything, but what actually happen if there is no return?

This discussion is preserved as it was. New answers, comments, and votes are closed.