I’ve been a bit confused on the return topic on Roblox Scripting. So I do know what return end does; makes the function not continue if it’s called.
But I’m confused on returning certain stuff, such as returning data in a function etc. Where is returning values used in, in just functions or also in other stuff? And what is the purpose of returning as example a piece of data or a string, etc in a function? And what is it exactly used for, how could it be handy to return values/anything, including on how I would use the data/value I have returned.
Thanks for any responses that could help me with this.
Sometimes, you want the function to process something in itself and expect something out of it. Return is the output of the function, after the input(function calling) is performed. Majority of these functions are usually for data and algorithms.
Returning is very important and extrememly useful, here’s how it works :
Let’s say you create a function to multiply a number by 2 (just an example)
function multiply(number)
number = number * 2
end
multiply(3)
Now it does work, it multiplies the number by 2 but… how do you get the result ? Well that’s exactly where you’ll need returning
function multiply(number)
number = number * 2
return number
end
local result = multiply(3)
print(result) --> 6
Here i’m assigning a variable to the function, in other words, to what the function will return
So this will become handy when you want to do the same task multiple times but with a different parameter (in this case the number)
Don’t worry i also was confused when i first discovered return but after practising a lot i understood how it worked and started to use it more and more, and today i can’t live without it lol
Return is used mostly for functions, but it can be used to stop loops. You use a function to manipulate data and then return to a variable.
-- Adds two numbers and returns the result
function addNumbers(a, b)
local result = a + b
return result
end
-- Usage
local num1 = 5
local num2 = 10
local sum = addNumbers(num1, num2)
print("The sum of", num1, "and", num2, "is:", sum)
True.the break command is used to stop or break any loops for it.
local function Calculate(Number1,Number2)
return Number1 + Number2
end
while true do
wait(1)
local Result = Calculate(math.random(1,99),math.random(1,99))
if Result > 50 then
print("The number "..Result.." is bigger than 50 so We are closed for now also cry about it"
break
else
print("The number "..Result.." is smaller than 50 so We are now opened till result is bigger than 50")
end
end
Well I dont see why you should use return. Its just a complicated way. You could just assign a new variable and add to that. It is way easier to understand and deal with and your code is more organized.
local result = nil
function multiply(number)
number = number * 2
result = number
end
multiply(3)
print(result) --> 6
Imo the hardest part about learning returning is to find an use for it, I recommend learning about module scripts (and storing functions in them) to really understand how useful returning is.
local function add(a, b)
return a + b
end
print(add(5 + 5))
Also I don’t think this example (and all the other math ones) is good, it gets the point across, but it doesn’t really show you how useful returning can be.
This was just an example of how it works, as this was the question and the purpose of this post. In other cases when you would need to use that function a large amount of time, you can’t just make 391 variables.
For a good example of something i recently used, i made a function to convert a color3 value into a table (because data store can’t save color3 values).
function toTable(color : Color3)
return {R = color.R, G = color.B, B = color.B}
end
A very simple function, but it would be very annoying to copy and paste it along the hole script, especially if for some reason i would want to change how it works. With the returning function i can just do that :
(Example)
local house = workspace.House
PlayerData.House.WallColor = toTable(house.wall.Color)
PlayerData.House.FloorColor = toTable(house.floor.Color)
PlayerData.House.RoofColor = toTable(house.roof.Color)
-- etc