Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

What is the purpose of returning?? Confused on what it would be used for, i'm a beginner.

Asked by 6 years ago

Hello, I have been watchin peasfactory, and I understand what he means when he explains returning. But, what is it useful for??

Heres his video about it------------

3 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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.
Ad
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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

0
Return is not a function User#24403 69 — 6y
0
@sjr04Alt When tf did I say its a function cmgtotalyawesome 1418 — 6y
0
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 200 — 6y
0
@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 cmgtotalyawesome 1418 — 6y
View all comments (4 more)
0
No User#24403 69 — 6y
0
@sjr04Alt I'm convinced that you literally don't know anything that is factual, actually stop commenting on answers lmao cmgtotalyawesome 1418 — 6y
0
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. User#24403 69 — 6y
0
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. User#24403 69 — 6y
Log in to vote
0
Answered by 6 years ago

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

Answer this question