I’ve been trying to learn returning for 2 weeks and I still don’t get it.I’ve watched countless tutorials like PeasFactory and AlvinBlox and I’m still confused about why it is useful. Also how or why is returning useful.
Let’s say you have a script:
function addNumbers(...)
local numbers = table.pack(...)
local sum = 0
for _, number in pairs(numbers) do
sum = sum + number
end
print(sum)
end
addNumbers(1, 3, 4)
This script adds the numbers together and prints it out. Right now we can only use the sum of the numbers from inside of the function. If you wanted to use the result somewhere else, you’re going to have to return it.
Returning pretty much converts the function call:
addNumbers(1, 3, 4)
Into the returned value. If you don’t return anything, it will be equal to nil. Here’s what the script would look like if you returned it.
function addNumbers(...)
local numbers = table.pack(...)
local sum = 0
for _, number in pairs(numbers) do
sum = sum + number
end
print(sum)
return sum
end
local sum = addNumbers(1, 3, 4)
We turned the function call into a variable. The variable is equal to the value we returned which is 8. You should test it in studio because my explanation is bad. This is useful because you can turn repeated calculations into functions to follow the D.R.Y. rule.
If you’re referring to the return
keyword it means what it looks like. When you use it in a function it halts the function and passes one or more values to where the function was called.
function addOne(num)
return (num + 1)
end
print(addOne(5)) --> 6
Tuples can be returned as well
function getSomeStrings()
return "Hello", "World"
end
local a, b = getSomeStrings()
print(a) --> Hello
print(b) --> World
It will halt the function at wherever it is called. If you don’t include anything after the keyword the return value will be nil. Any function that doesn’t use return
will also return nil.
function loop()
for i = 1, 10 do
print(i)
if (i == 5) then
return -- The function will not print anything after 5. 'break' also works here.
end
end
end
function stuff()
print("start")
return
print("stop") -- Stop will not print. Studio might highlight this line because it is 'un-reachable'
end
local function send10()
return 10 -- this will return the number 10
end
local number = send10()
print(number) -- the print will show the number 10
It simply returns the value you ask the function to return.
A function return
ing a value basically means the function is output
ing a value.
If we had a function that squares an input that we gave (using parameters) (cubing a number meaning raising it to the 3rd powee)
local function cube(n)
return n^3
end
Mow that we have this function defined, anytime I call it it will run its code and return (output) a value, we can store that value inside of a variable, print it, or do whatever we want with it.
local a = cube(2)
print(cube(3)) --prints 27, because 3^3=27
print(a) --prints 8, 2^3=8
Really you just have to imagine cube(3)
for example being replaced with the returned value. Of course you can return any type of value, even objects, here we’re returning a number
It is also important to note that after a function returns something, any code beyond that would not run and the function will stop and the script contiues
local function hi()
return "hi"
print("value returned") --if im not mistaking this would be underlined.
end
And this is useful, for example if you wanted to have a function that checks whether a part is invisible or not (check if transparency == 1, return true meaning its invisible, else if transparency == 0, return false meaning visible.
local function IsInvisible(part)
if part.Transparency == 1 then
return true --it will return true, and stop, so it will not check the next elseif
elseif part.Transparency == 0 then
return false
end
end
The fact that return
can stop a function from finishing the code after, can also be used in a way, to make it so if a condition inside of the function is not met, just stop the function and do absolutely nothing. Like if we had a function that changes the brickcolor of a part, and we wanna only change the colour of parts that are named "Bob"
, else we do nothing.
local function ChangeColour(part)
if part.Name ~= "Bob" then return end --you'll find this expression commonly written like this, it's checking if the part's name is not Bob, and if so just return (no need to return a value, just empty, or otherwise nil), and stop the function from proceeding
part.BrickColor = BrickColor.new("Really Red")
print("Changed Bob's color successfuly!")
end
There is even more! You can return multiple values, like the following script.
Let’s just say you wanted to check if a part’s color was not red, if it’s not, then you return true, and also return the color of that part, two information to return!
local function IsNotRed(part)
if part.BrickColour == BrickColor.new("Really Red") then
return false
else
return true, part.BrickColor --the two values are seperated with a comma
end
end
If you wanna recieve back the two values, you need to do it this
local part = workspace.Part --our part
local isred, colour = IsRed(Part) --notice the way we wrote this!
--isred would be the first value that we returned, in this case the boolean (true or false)
--colour would be the second value that we returned, in this case the brickcolor
print("Is the part red:", isred)
print("The Part's colour:", colour)
Say I have a function in my script like this:
-- Simply finds a player from a name string.
function findPlayer(Name)
local Player = Players:FindFirstChild(Name)
end
Now I want to make use of this function without having to do some hacky method like
local PlayerFound
function findPlayer(Name)
local Player = Players:FindFirstChild(Name)
PlayerFound = Player
end
findPlayer("PlayAsync")
Instead I can just return the Player, like this:
function findPlayer(Name)
local Player = Players:FindFirstChild(Name)
return Player
end
local PlayerFound = findPlayer("PlayAsync")
print(PlayerFound.Name)