Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can someone explain what returns are for?

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

✓ Accepted answer

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.

2 comments

0

Thanks, so then what could you use that variable for?

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.