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

What does return do and how does it work out in everyday scripting? [closed]

Asked by 5 years ago

This question already has an answer here:

Return

I've just started scripting and when I was learning about functions, I came across returns. I need help understanding what it does.

0
https://scriptinghelpers.org/questions/532/return an old but good explanation of return. DinozCreates 1070 — 5y

Marked as Duplicate by zblox164, Ziffixture, Fifkee, Gey4Jesus69, and DeceptiveCaster

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?

1 answer

Log in to vote
0
Answered by
maxbd 44
5 years ago
Edited 5 years ago

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!"

Ad