I've just started scripting and when I was learning about functions, I came across returns. I need help understanding what it does.
COMMUNITY QUESTION
What does return do and how does it work out in everyday scripting?
1 answer
The return keyword returns a value as implied by it's name. Also, when return is called, the function stops and exits where it is at.
For example, if you wanted a function to give a result you could use the return keyword.
Say in this example, you want assign the value "Hello, World!" to a variable if a variable is true, using a function of course.
-- Script (on server)
local var = true
local function coolFunction()
if var == true then
return "Hello, World!"
end
return false -- You can also say 'return' instead of 'return false'
end
var = coolFunction()
print(var)
In the example shown above, the var variable will be assigned the return value of the coolFunction function if it is equal to true. Then we print out var's value because why not.
Because var is originally true, it will return "Hello, World!" and not get to where the function returns false.
Output: "Hello, World!"
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
https://scriptinghelpers.org/questions/532/return an old but good explanation of return.