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

Can someone explain Return to me?

Asked by 9 years ago

I have seen return in a few scripts and I don't quite get what it does. I have looked it up in the wiki but I don't quite get it. Can someone explain it to me?

1

1 answer

Log in to vote
5
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

Return is applicable in many ways. You can get values on a long formula, but you only have to type in the formula once. For example:

Instead of

x = math.sqrt(math.pow(3 ,2) + math.pow(4 ,2))
y = math.sqrt(math.pow(6 ,2) + math.pow(12 ,2))

print(x)
print(y)

You can simplify that into one function:

x = 3
y = 4

p = 6
q = 12

function PT(a, b)   -- Pythagorean Theorem
    Answer = math.sqrt(math.pow(a, 2) + math.pow(b, 2))
    return Answer
end

print(PT(x,y))
print(PT(p,q))

Of course, there are other useful things to do with return, but this is just one example.

The gist is that you're literally converting a function into a value.

Take note that a return statement immediately stops the function, so anything besides ends after it will error out.

0
Oh. I guess that makes sense! Thanks! minikitkat 687 — 9y
1
You're welcome. Redbullusa 1580 — 9y
Ad

Answer this question