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

I don't think I understood the meaning of returning?

Asked by
XDvvvDX 186
5 years ago

Hey. I learned returning when I was a starter, and it's all fine. But lately I feel I do not know the meaning of returning and what it does so well. I use it rarely, and I use a lot of functions in my scripts. I feel maybe I got the wrong knowledge from a wrong website, or whatever. Can someone tell me what they are used for and give me an example? Thanks.

0
they return a piece of code Gameplayer365247v2 1055 — 5y

4 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

The return keyword prepares the function to give back a result. In most cases, with no procedures that provide a finished result, these functions just become what’s known as a void method.

Let’s take a look at a few examples of where we would apply return in daily code:

01--// Mathematics
02local function multiplyNumber(number, multiple)
03    return number*multiple
04end
05 
06local fiveTimesFour = multiplyNumber(5,4)
07print(fiveTimesFour) --// 20
08 
09--// Boolean condition
10local function booleanCondition(Expression)
11    if (Expression) then
12        return true
13    end
14    return false
15end
16 
17if (booleanCondition(workspace.Part:IsA("Part")) then
18    print("Is a Part!")
19end

Note:

return will stop the function immediately, anything within the scope below will not run.

Ad
Log in to vote
0
Answered by 5 years ago

here you can understand with my script:

1function rturn()
2 print("hi")
3 return 1
4end
5 
6game.Workspace.scarp.Transparency = rturn()

When returned is active, it stops the function like a break in loops, return turns the function into a variable, return gave the function variable 1 to its value, therefore we change the transparency of Scrappy The Part. You can store the function in a variable like

1Rat = rturn()
2game.Workspace.scarp.Transparency = Rat
Log in to vote
0
Answered by
Nckripted 580 Moderation Voter
5 years ago

Return is a good way to get data from a function. You won't need it too often, but every once in a while is a good thing. For example:

01function returnHumanoid(part)
02    local humanoid = part.Parent:FindFirstChild("Humanoid")
03 
04    if humanoid then
05        return true
06    else
07        return false
08    end
09end
10 
11local isHumanoid = returnHumanoid(randomPart)
Log in to vote
-3
Answered by
Xapelize 2658 Moderation Voter Community Moderator
5 years ago

return is a thing which returns values such like this:

1local function plus(A,B)
2    return A*B
3end
4 
5local number = plus(1,1)
6 
7print(number)
8 
9--> the thing got printed is 2
0
If you’re answering a question, please go into depth. Your answer will also be 1 to be frank as you’re using the asterisk operand—multiplication operator. Ziffixture 6913 — 5y

Answer this question