Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

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

Asked by
XDvvvDX 186
6 months 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 884 — 6mo

4 answers

Log in to vote
0
Answered by
Ziffixture 5707 Moderation Voter Community Moderator
6 months ago
Edited 6 months 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 6 months 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 6 months 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 709 Moderation Voter
6 months 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 5707 — 6mo

Answer this question