What is the purpose of returning?? Confused on what it would be used for, i'm a beginner.
3 answers
#Return
Return is a keyword, and what it does is either return a value from a function, prematurely end a function, or both. ##Returning values
local function getTriangleArea(base, height)
return (base * height)/2
end
local area = getTriangleArea(7, 4)
The area variable holds the "result" of the function. In this case the result is 14, because the function returns the area of a triangle. Not all functions have return values. Another example of a function that has a return value is wait. Now this function has two return values; the delta time (time waited) and the time since the program started.
local dt, timeSinceProgramStarted = wait(3)
print(dt, timeSinceProgramStarted)
--> 3.00038384925, 3838.9 (just an example, you will get different numbers)
##Prematurely ending functions You can prematurely end a function, if, for example, a condition is NOT met.
local pizzaPrice = 15
local function orderPizza(customer)
if customer.Money.Value < pizzaPrice then
return -- return out of the function if they do not have enough money
end
-- Function won't even reach here if the condition above was met.
customer.Money.Value = customer.Money.Value - pizzaPrice
-- give them their pizza
end
######Hopefully this answered your question and if it did, then don't forget to hit that "Accept Answer" button. If you have any other questions, then feel free to leave them down in the comments below.
Return is useful for many things. It can be used for not letting a player run through a script like this:
local canRun = false
while true do
if canRun ~= true then return end -- this would restart the script.
end
EDIT: If you don't have a condition in which your script will run, calling return will stop the script forever. For example, this script would run halfway through, and then never run again:
local a, b = 1, 2
if a + b ~= 4 then return end
--random script (it doesn't matter whats down here because it won't run after the return
return can also give us a value when used in a function, like this:
function add(a, b)
return a + b -- this would return the value of whatever a + b equates to
end
print(add(3, 4))
--the output would be 7
Hope this helped you out
-Cmgtotalyawesome
8 comments
Return is not a function
@sjr04Alt When tf did I say its a function
So using return in a while loop if conditions aren't met restarts the script? For example, let's say you have a bool value change whenever the player is within a certain range of a part. If it's false, then the script would restart and check for it again?
@ScrubSadmir Yup, thats exactly how it works, however a return is not needed in that situation as you can use an if statement on its own. You could use it for while loops though though, whatever looks nicer and is easier to read for you though
No
@sjr04Alt I'm convinced that you literally don't know anything that is factual, actually stop commenting on answers lmao
Return isn't even a function, you're saying it is in "The return function is useful for ...". And in your edit you proceed to say "calling return will stop the script forever ..." which is all wrong.
And if you do not know how to use the correct terminology let alone answer a question correctly then don't answer at all. Also, really, no need for a signature to end off your answer. Your username is clearly visible at the top.
giving or returinig gives back information when the function is called heres an example but this is useless
function add(num1,num2)
return num1 + num2
end)
print(add(1, 2))
3
this is giving back the answer it also stops the function if there was code after the return it wouldn't run
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Accidently removes the link soo here, https://www.youtube.com/watch?v=jBjIs4TUjeY&index=7&list=PLXX6hhg4CysYf0M-_GtCEOgGlkBfrXsoJ