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 10 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
10 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

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

You can simplify that into one function:

01x = 3
02y = 4
03 
04p = 6
05q = 12
06 
07function PT(a, b)   -- Pythagorean Theorem
08    Answer = math.sqrt(math.pow(a, 2) + math.pow(b, 2))
09    return Answer
10end
11 
12print(PT(x,y))
13print(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 — 10y
1
You're welcome. Redbullusa 1580 — 10y
Ad

Answer this question