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

How can I use return in functions?

Asked by
Oficcer_F 207 Moderation Voter
6 years ago

I always see return in scripts, and yes I know that they give a value to a function, but what can we use it for? Does anyone have a real-world example of what you can use it for? And can someone explain it to me better? Thanks!

4 answers

Log in to vote
1
Answered by
Pojoto 329 Moderation Voter
6 years ago

When you use return, you're essentially storing a value to the function.

For example, typing

return 10

in a function sets the value of that function to 10, so that when you print the function you'll see the number 10 printed.

Take a look at this function.

1local function practiceFunction()
2    return 15
3end
4 
5print(practiceFunction())

We returned 15 to the function named practiceFunction, and then we printed practiceFunction, showing 15 in the output.

You can also return strings/text, boolean values like true and false, tables, and other data types.

Returns are useful is you want to get a value out of a function, but not use it right away.

1local function add(x,y)
2    print(x+y)
3end
4 
5add(5,7)

Instead of printing 12 directly, we could store 12 into the function using return, letting us use 12 whenever we want to.

1local function add(x,y)
2    return(x+y)
3end
4 
5--Many lines later...--
6 
7print(add(5,7))

Returns are also used to escape a function if you don't add anything after it.

01local number = 6
02 
03local function check()
04    if 5+number == 12 then
05        return
06    else
07    print("Change your number to 7")
08    end
09end
10 
11check()

If the variable number is 7 then it will return, exiting the function, not running the rest. However, if the variable number isn't 7 then it will print.

0
Thanks! But can you give me an example of whst you would use this for in a gane? (No code) Oficcer_F 207 — 6y
0
game* Oficcer_F 207 — 6y
0
We could make a function that checks if a randomly generated number is equal to 4. If it is equal to 4 then we can return true, if not, then return false. That will set the value of the function to either true or false, so we can use it later in our script. Pojoto 329 — 6y
Ad
Log in to vote
0
Answered by
F_F 53
6 years ago
1function test(w)
2    return w
3end
4 
5local func = test(workspace)
6 
7 
8print(func)

you can use it for returning values from a function

Log in to vote
0
Answered by 6 years ago

Heres an Example.

1function xD()
2if workspace.FilteringEnabled == true then
3return
4else
5print('WHY ISNT YOUR GAME FE!!??!!?!?!?')
6end
7end
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Return is pretty much an if else statement so,

1function test() -- your function you can name it anything
2    if part.Transparency == 0 then -- just looking for a variable you can do any property
3        print("Transparency = 0")
4    return
5    else -- if thats not true it will output to this
6        print("Transparency test failed")
7    end
8end

Answer this question