Return functions

I always see “return” in functions, but have no idea what it’s useful for or what it even does.

Can somebody give me like 2 examples of a good use for return?

4 Likes
4 Likes
local function returnValue(value)
   return value^2
end

local value = 2
print(returnValue(value)) --> 4
1 Like

Return can be used to return a value back to a function to make that function become the value that you return to the function. It also can be used to break a function.

Break the function:

local function breakFunc(a)
	if a == false then --- if a is false then it will return nothing and make the function stop working
		return
	elseif a == true then
		print('Hello, World!!')
	end
	print('Finished Function...')
end

breakFunc(false)

Return a value to a function:

local function returnStringToFunc()
	return 'Hello, World!!' --- You can return anything to the function to make the function become something by using return!!
end

print(returnValueToFunc()) --- return "Hello, World!!" because we return the "Hello, World" string.

local function addNumber(num1, num2)
	return num1 + num2
end

print(addNumber(1, 2)) --- print 3 because we return the sum of num1 and num2.

local function returnTableToFunc()
	--- return a table to the function to turn the function into a table.
	return {
		'Hello, World!!';
		true;
		false;
		1;
	}
end

for k, v in ipairs(returnTableToFunc()) do --- v for values and k for keys
	print(v) --- print all of the values in the table
end
2 Likes

(Copy paste response from when I answered this previously here)


Returning within a function is basically sending a value to whatever called it, I guess.
For example, here is a (useless) function that adds to numbers for whatever reason:

local function add(num1, num2)
    return num1+num2
end

local added = add(5,2) --any numbers
print(added) --7


Ok i really suck at explaining it, so here is the roblox article on it: https://education.roblox.com/en-us/resources/returning-values-from-functions

RemoteFunctions or BindableFunctions are good examples.
The code below will return player mouse position to the server.

local func = script.Parent
func.OnServerInvoke = function()
 return game:GetService("Players").LocalPlayer:GetMouse().Hit.Position
end

return allows you to send back variables/arguments that are created within a function to be used outside of it.

For example:

local function addNumbers(a, b)
    local result = a + b
    return result
end

local c = addNumbers(2, 3) -- 2 + 3 (a + b)
print(c) -- Output: 5

You can easily return multiple variables as well!

local function myFunction(a, b)
    local result1 = a + b
    local result2 = a - b
    return result1, result2
end

local added, subtracted = myFunction(9, 4) -- 9 + 4 (a + b), 9 - 4 (a - b)
print(added, subtracted) -- Output: 13, 5

And if you want to ignore a returned value from the function you would use _ like so:

local function myFunction(a, b)
    local result1 = a + b
    local result2 = a - b
    return result1, result2
end

local _, subtracted = myFunction(9, 4) -- 9 - 4 (a - b)
print(subtracted) -- Output: 5

Hope this helps.

2 Likes