Returning a function inside another function

If you have 2 functions and one function inside the other, how can you return on the first one?

function a()

function b()

–return a?
end
end

I simply want to stop further execution of a.
I know there are some possible ways such as setting a variable and adding a conditional but is there a way to quickly return the first function?

1 Like

Not quite sure what you’re trying to ask, but if you want b to return a, you need to have a return something. For example:

function a()

    return (1 + 1)

end

function b()

    return a()

end

B is returning A, which returns 1+1. Calling b() would return 2.

Im saying a function inside a function

I would not recommend doing that. I guess if you really want to you can. What do you need it for?

By all means, you won’t get an error. You may be confused if you do it though, with all the statements found.

I want to stop further execution of the first function

like this:

function a()
     local function b()
          return 1 + 1
     end

     b()
     return -- right after you call b in the function
end

I meant doing it inside the second function and not using the first function

I stated in my post:

function a()

function b()

–return a?
end
end

1 Like

idk, doing that doesn’t really make sense unless you tell us what you need it for

Im using Activated events and determining each button’s name then using the name to determine if further function execution is needed

hmm, well i cant help you unless you show me exact the code you are using

How is code going to help anything? I already showed in my post what I was asking for. I dont need help with my code, I need help with general scripting functions that utilizes a return of the first function within the second function

what your asking for doesn’t make any sense. i need your code so i can fix your problem

I’m assuming you mean something like this. Yes you can if it is.

local function a()
    return 4
end

local function b()
    return a -- "a" holds our function, so we can call it normally
end

local functionResult = b() -- a is returned, so we can do the following:
print(functionResult()) -- should print 4!

It’s like returning any other value, really.

I want to do the equivalent of:

local a = false
function a()

function b()

if 5 > 2 then
a = true
end

end

if a then
return
end
end

But all within the second function

there is no way to return out of a parent function from inside a child function, return only works in the function its called in

1 Like

Let’s say we have functions a and b. Function b is nested within function a, and you want function b to be able to stop execution of function a. You can return a flag from function b to determine whether or not to return within function a.

Example:

local function a(): nil
    local function b(): boolean
        return true;
    end
    if b() then
        return;    --This will break out of a if b returns true
    end
    --Other stuff
end
6 Likes

Seems like a good alternative solution, thanks for posting :smiley:

1 Like

I know it’s been a year but what does it mean when you put the colon after the function?
Like when you did

local function a(): nil

what does “: nil” mean