I never really understood returns, when you would use them, and if they are important. As far as I know, they are pretty important, but all the examples I have seen showing how they work make them seem useless. Can anyone try and help me understand, give me an example or analogy, something to see what the point of returning is?
“returning” is the act of sending info back to where the function was called from. It also ends the function (doesn’t execute anything after it) Example:
function multiply(x,y)
return x*y
print("hi")
end
local product=multiply(2,2)
print(product)
--output is 4
The behavior of stopping the function can also be used as an easy “stop here” command:
function doThing(x)
if x>10 then
print("x is greater than 10")
return
end
print("x is less than 11")
end
doThing(4)
--output is "x is less than 11"
doThing(11)
--output is "x is greater than 10"
An example of a basic function would be a function that takes a number and returns that number multiplied by two.
In maths this would be represented as
f(x) = 2*x
Here are a few examples of this function in action
f(1) = 2
The input is 1 and the result is 2, as expected
f(5) = 10
So five multiplied by 2 gives you 10
In Lua the return keyword is simply used to return a value from a function.
So the function I described above could be implemented as follows in Lua:
function f(x)
return x*2
end
This function will take an input value “x” and return two times that value.
So if you want to see what the result of the function f is for the input 5 you would do the following in Lua.
-- Print is a function that outputs text to the output window in Roblox
print(f(5))
This would output 10, so what the return keyword does is it allows the calling location of the function to access the result of the function. In Lua functions can take multiple inputs and also return multiple results.
So what is the point of returning?
Functions that return a value can be used for code organization purposes, it is possible to never use the return keyword but you would likely not end up with good code. A function can do a specific computation and then return the value for use in other parts of your scripts.
You “give” something to a function (i.e. arguments), and it “returns” stuff to you.
You give an oven a frozen pizza, and it returns a cooked pizza.
If you want a more practical example for a game, here is one for when the user has a certain amount of ‘XP’, and you create a function that calculates their ‘level’, based on the amount of ‘XP’ they have collected.
function getLevel(xp)
return math.floor(xp / 100)
--returns a number back to the main code based on what you 'fed in' to the function
--this could be any math you want it to be!
end
Then, in the main part of your code, you run the function above, and it “returns” what level they are.
level = getLevel(player.XP.Value)
print("The player's level was "..level)
level = getLevel(150)
print("If you have 150 XP, your level would be: "..level)
Just picking at this part specifically; it’s not that the code after the return here won’t run, it’s that it won’t compile at all. Consider wrapping the return
in a do end
for the intended behavior (or lack thereof).