What does return function mean

Hey guys im not sure if i’m on the right topic let me know if i am not.

I’ve been analyzing this code in the classic sword script and i saw return function(data) when there was no other function in that script or any other one so what does return function(data) mean?

can you send the code snippet where this occurs

Here’s a very good post that goes into detail on return:

In short, it’s main use is to, as the name implies, return some sort of value. Though, it can be used as a way to terminate a function as well. After looking at the code you’re referring to though, it is used to return a function that the Create function creates.

function Create(ty)
	return function(data)
		local obj = Instance.new(ty)
		for k, v in pairs(data) do
			if type(k) == 'number' then
				v.Parent = obj
			else
				obj[k] = v
			end
		end
		return obj
	end
end

The Create function is called upon in this line of code:

local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
	Name = "R15Slash",
	AnimationId = BaseUrl .. Animations.R15Slash,
	Parent = Tool
})

The ty parameter is to the "Animation" string, while the brackets indicates the data (it’s a table).

-- Visually, that looks like this:
Create(ty)(data)
2 Likes

The return keyword, much as its name suggests, returns a value. What may be confusing you here is that functions are actually values too!

This means we can create a codeblock, and have it return a value using the return keyword, and in this case, we can make it return a function.

return can be used in functions and module scripts.

Here’s an example of return being used in a function:

function add(a,b)
    local sum = a + b
    return sum
end

If we give this function 2 numbers, we will get the sum of the 2 number in return (pun intended).

local result = add(5,3)
print(result) -- 8, because 5+3=8

Similarily we can return functions:

function test()
    return function(a,b)
        local sum = a + b
        return sum
    end
end

If we call test in this example, we are given the function add in return:

local add = test()
local result = add(5,3)
print(result) -- 8, and because 5+3=8

I hope this help!

1 Like

thank you! I really appreciate it!

1 Like

You can also attach a table and grab info from that. Everything from strings, to more tables, or even functions or threads.

Formatting can get pretty crazy.

function SomeFunction(ReturnVal, call)
    if ReturnVal then
        return function(Tab)
            if not table.find(Tab, ReturnVal) then
                warn("Value not found in extra table!")
                return nil
            end
            return if call then Tab[ReturnVal]() else Tab[ReturnVal]
        end
    else
        print("Ran with no added table.")
    end
end

SomeFunction()
-- prints "Ran with no added table."

local Apple = "A red apple!"

local Banana = {Color = "Yellow", Name = "Banana"}

local function Lemon()
    print("A sour, yellow lemon!")
end

print(SomeFunction("Apple"){
    ["Apple"] = Apple;
    ["Banana"] = Banana;
    ["Lemon"] = Lemon;
})

print(SomeFunction("Banana"){
    ["Apple"] = Apple;
    ["Banana"] = Banana;
    ["Lemon"] = Lemon;
})

SomeFunction("Lemon", true){
    ["Apple"] = Apple;
    ["Banana"] = Banana;
    ["Lemon"] = Lemon;
}

It’s definitely not advisable, but you could take it a couple steps further and practically just emulate other languages like Javascript and C++ if you structure your code properly lol

Keep in mind that for certain functions/events, returning values such as deep/mixed tables may not give you the expected result (instances such as RemoteEvents come to mind)

1 Like

Right, it’s pretty uncommon to really need to use functions like this anyway. Maybe if you’re setting up some sort of on-the-fly match function?

They’re fun to play around with, though.

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