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
4 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 — 4y

4 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 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:

--// Mathematics
local function multiplyNumber(number, multiple)
    return number*multiple
end

local fiveTimesFour = multiplyNumber(5,4)
print(fiveTimesFour) --// 20

--// Boolean condition
local function booleanCondition(Expression)
    if (Expression) then
        return true
    end
    return false
end

if (booleanCondition(workspace.Part:IsA("Part")) then
    print("Is a Part!")
end

Note:

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

Ad
Log in to vote
0
Answered by 4 years ago

here you can understand with my script:

function rturn()
 print("hi")
 return 1
end

game.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

Rat = rturn()
game.Workspace.scarp.Transparency = Rat
Log in to vote
0
Answered by
Nckripted 580 Moderation Voter
4 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:

function returnHumanoid(part)
    local humanoid = part.Parent:FindFirstChild("Humanoid")

    if humanoid then
        return true
    else
        return false
    end
end

local isHumanoid = returnHumanoid(randomPart)
Log in to vote
-3
Answered by
Xapelize 2658 Moderation Voter Community Moderator
4 years ago

return is a thing which returns values such like this:

local function plus(A,B)
    return A*B
end

local number = plus(1,1)

print(number)

--> 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 — 4y

Answer this question