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

What is a good example of returning? What does returning do exactly?

Asked by 4 years ago

I've been learning to script, and I've gone over functions but I can't really get a good picture of what returning a function would really be used for.

I've also gone over numerous posts about returning and I still don't really get it. If someone can explain this thoroughly and easily, I'd love to have a simple answer and example to what returns are

3 answers

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
4 years ago

Returning inside a function basically makes it stop and return a value that can be attributed to a variable

Lets say that I have this function:

1function getRandomPlayer()
2    local players = game.Players:GetPlayers()
3    local selected = players[math.random(1, #players)]
4 
5    return selected
6end
7 
8local plr = getRandomPlayer()
9print(plr.Name)

In that case, we are calling a function that use return to "export" a variable from inside it.

Another way to use return is to stop a function from running:

01local check = false
02 
03function func()
04    if not check then
05        return -- stops the function if variable is false
06    end
07    print("Function running...")
08end
09 
10func()

If this helps, please upvote and mark this answer as accepted

0
One question, what would be the expected output from the first way you showed? Lilycover87 8 — 4y
0
The expected output is, the player's name chosen. If there are 10 players, it would choose 1 and it'll print the chosen player's name. Gabe_elvin1226aclan 323 — 4y
Ad
Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

Returning is used to send informations back to where the function is called, here is an example:

1local function sayHi()
2    return "Hi!"
3end
4-- Print returned value of sayHi
5local returnedValue = sayHi()
6print(returnedValue)

Here is the expected output:

Hi!

You can also return using integers, as well as performing arithmetics.

1local function multiply(x, y)
2    return x*y
3end
4 
5local returnedValue = multiply(5, 5)
6print(returnedValue)

25

Returning is very simple and easy to learn. Quick Recap: Returning is used to send informations to where the function is called, so whatever your returned value is, that value will get sent.

0
so basically if you put the function in a variable the return will come out of the output? Lilycover87 8 — 4y
1
You don't need to put the function in a variable, but incase you want to do you can. You can just simply do 'print(multiply(5, 5))' Gabe_elvin1226aclan 323 — 4y
Log in to vote
1
Answered by
zadobyte 692 Moderation Voter
4 years ago

Returns are basically what you can get out of a function. Let's compare it to going to the store and paying for an item.

1local function giveMoney(money)
2    if money >= 10 then
3        return "Some sweet shoes!"
4    end
5end
6 
7giveMoney(10)

In the function giveMoney, it checks for the amount of money given. The example shows that you gave the function (or shopowner) 10 dollars. Since the shoes are 10 dollars, you are given some sweet shoes. Once that happens, you can show them off!

1local function giveMoney(money)
2    if money >= 10 then
3        return "Some sweet shoes!"
4    end
5end
6 
7local shoes = giveMoney(10)
8print(shoes) -- Some sweet shoes!

Answer this question