What does "return function()" mean?

  1. What do you want to achieve? Keep it simple and clear!
    I want to be able to understand and learn how they work and how to use them.
  2. What is the issue? Include screenshots / videos if possible!
    I am having trouble on figuring at what they actually do.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried looking on the dev forums but it doesn’t give me a clear understanding

Can you guys please help me with “return function()”? Maybe add an example and what they do and how they work.

7 Likes

In Roblox Lua scripting, the phrase “return function()” is often used to define and return an anonymous function. This can be useful for creating closures or for defining functions on the fly. For example:

function createMultiplier(factor)
    return function(x)
        return x * factor
    end
end

local double = createMultiplier(2)
print(double(5)) -- This will output 10

In this example, “return function(x)” defines an anonymous function that takes an argument ‘x’ and multiplies it by the ‘factor’ specified when creating the ‘double’ function. This concept allows you to create flexible functions in your Roblox Lua scripts.

3 Likes

This makes a little bit more sense, but is X equal to 5 or 2?

2 Likes

‘x’ is equal to 5 when we call the ‘double’ function with the argument ‘5’ here:

print(double(5)) -- This will output 10

So, in this specific instance, ‘x’ is indeed equal to 5, and the function returns 10, which is the result of multiplying 5 by the ‘factor’ (which is 2 in this case).

2 Likes

Is it necessarily in order as well?

2 Likes

I don’t know what you mean. Can you give me an example?

2 Likes
local function Test()
      return function(x)
end

Since Test is the top, it will be the first number, and since return function(x) is the bottom, its the second number?

4 Likes

No these are the parameters of a function.

2 Likes

Can you tell me what you don’t understand so I can get into it.

2 Likes

I am asking where it goes, which is most important about my question.

Is return function() connected to the local function?
Like:when you call it, does return function() connect to it?

Test(){ --Called function
'Hello' --Table is return function?
}
1 Like

Yes the returned function is connected to the calculate function

1 Like

It basically just returns an anonymous function.

Another example:

function test(text)
	return text
end

print(test("Test")) — Prints Test because the text parameter given was returned

Yes, that makes lots of sense now.
Can you please show me where the return function is? Just a refresh.

function createMultiplier(factor)
    return function(x)
        return x * factor
    end
end

local double = createMultiplier(2)
print(double(5)) -- This will output 10

The function comes after return so when you connect the function to a variable, you can access that function.

Do you have anymore questions?!

Nope, it’s all good. Thank you so much!

You can check the documentation if you forget something: Functions | Documentation - Roblox Creator Hub

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.