For example:
01 | thing = false |
02 |
03 | function okay() |
04 | wait( 2 ) |
05 | if thing = = false then |
06 | print ( "Hello World!" ) |
07 | thing = true |
08 | return okay() |
09 | elseif thing = = true then |
10 | print ( "Goodbye!" ) |
11 | end |
12 | end |
If it works correctly, it should print hello world, and then goodbye.
return
"returns" a value of a function. For example:
1 | local function returnOne() |
2 | return 1 |
3 | end |
The function above will return 1. Example:
1 | print (returnOne()) --> 1 |
2 |
3 | print (returnOne()+ 10 ) -->11 |
However functions are useful because you can give them arguments, and you can return different things based on the arguments.
1 | local function evenNumberCheck(num) |
2 | return num% 2 = = 0 |
3 | end |
The above function will check if numbers are even, and return a bool based on that. Example:
1 | print (evenNumberCheck( 1 )) --> false |
2 |
3 | print (evenNumberCheck( 6 )) --> true |
4 |
5 | if (evenNumberCheck( 8 * 2 )) then |
6 | print ( "8*2 makes an even number" ) |
7 | else |
8 | print ( "8*2 doesn't make an even number" ) |
9 | end |
For more info, I suggest reading this answer to the same question.
You would use return to get the value of a function.
01 | local a = true |
02 | local b = true |
03 |
04 | function ReturnValues() |
05 | return a and b |
06 | end |
07 |
08 |
09 | function PrintValues() |
10 | print ( tostring (ReturnValues())) --tostring converts things to strings |
11 | end |
12 |
13 | PrintValues() |