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

Is this how you use the return function?

Asked by 8 years ago

For example:

01thing = false
02 
03function okay()
04wait(2)
05if thing == false then
06print ("Hello World!")
07thing = true
08return okay()
09elseif thing == true then
10print("Goodbye!")
11end
12end

If it works correctly, it should print hello world, and then goodbye.

0
after 2 seconds. MustangHeart 67 — 8y

3 answers

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

return "returns" a value of a function. For example:

1local function returnOne()
2    return 1
3end

The function above will return 1. Example:

1print(returnOne()) --> 1
2 
3print(returnOne()+10)-->11

However functions are useful because you can give them arguments, and you can return different things based on the arguments.

1local function evenNumberCheck(num)
2    return num%2==0
3end
%, or modulus, gives the remainder.

The above function will check if numbers are even, and return a bool based on that. Example:

1print(evenNumberCheck(1)) --> false
2 
3print(evenNumberCheck(6)) --> true
4 
5if(evenNumberCheck(8*2))then
6    print("8*2 makes an even number")
7else
8    print("8*2 doesn't make an even number")
9end

For more info, I suggest reading this answer to the same question.

Ad
Log in to vote
2
Answered by
farrizbb 465 Moderation Voter
8 years ago

You use return to get the value of a function and to stop it

Log in to vote
0
Answered by 8 years ago

You would use return to get the value of a function.

01local a = true
02local b = true
03 
04function ReturnValues()
05return a and b
06end
07 
08 
09function PrintValues()
10print(tostring(ReturnValues())) --tostring converts things to strings
11end
12 
13PrintValues()
0
so basically, does it go back to the start of the script? (or as specified?) MustangHeart 67 — 8y
0
it'll go back to where it was called.  Azarth 3141 — 8y

Answer this question