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.
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
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
return will stop the function immediately, anything within the scope below will not run.
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
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)
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