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

Can someone explain what returns are for?

Asked by
vinboi 0
9 years ago

I understand how returns work but I don't understand what they are used for. If a person and print a variable to they output, why would they want to return it instead?

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

return is used in functions to stop everything (loops, etc.) and, literally, return a value.

function add(a, b)
    return a + b
end

local sum = add(2, 2)
print( sum ) -->4

So basically, add runs, then it gives us the sum. This lets us set a variable to the function's result, as we did on line 5.

Comment with questions.

0
Thanks, so then what could you use that variable for? vinboi 0 — 9y
0
The 'sum' variable equals what the function returned - in this case, it's the result of the addition. We use it to store the result, so we don't have to keep calling add() each time we want 2+2; we can just use the variable. Perci1 4988 — 9y
Ad

Answer this question