I know the keyword return stops the execution of a function and returns a value but I don't know how to really use it, any in game ideas of it's usage?
https://scriptinghelpers.org/glossary/return
http://wiki.roblox.com/index.php/Function#Using_Return
http://www.lua.org/pil/4.4.html
return
does stop executing the containing function when the return
statement is reached, but that's not really the point.
In Lua (as in most programming languages) the term function
really refers to two different things.
In math, a function is something that takes values in, does something, and gives you values back.
For example, the "square root" function takes in a number and spits out the square-root of that number.
In Lua, you use return
to state which value you give out. For example, here is a square
function which returns the square of a number:
function square(x) -- x is the 'in' number return x * x -- `x * x` is the 'out' value end print(square(0)) --> 0 print(square(1)) --> 1 print(square(2)) --> 4 print(square(3)) --> 9 print(square(4)) --> 16 print(square(5)) --> 25
An important part of mathematical functions is that the "out" value depends only on the "in" value. For example, the square(5)
is always going to be 25
-- nothing can change that.
On the other hand, workspace:FindFirstChild("Part")
can give you different objects back -- because it depends on the state of the world. It is not a mathematical function. (See below)
(Also called routines, subroutines, and methods in the context of objects)
In programming, a procedure is a block of code that is packaged together. These do something (called "having side-effects").
For example, this announces a player has joined the game:
function announcePlayer(name) local hint = Instance.new("Hint", workspace) hint.Text = "Welcome " .. name .. "!" wait(5) hint:Destroy() end
Procedures may use return
in order to quit before reaching the end. This pattern is called an early return.
For example, here's a "kill toucher" function that uses early return:
function killToucher(toucher) local human = toucher.Parent and toucher.Parent:FindFirstChild("Humanoid") if not human then return -- quit early, because the rest won't work! end human:TakeDamage(20) end
When you just write return
, you're essentially writing return nil
. Since nil
often represents absence, you're allowed to omit it.
https://scriptinghelpers.org/glossary/return
[http://wiki.roblox.com/index.php/Function#Using_Return]****(http://wiki.roblox.com/index.php/Function#Using_Return)
http://www.lua.org/pil/4.4.html
return
does stop executing the containing function when the return
statement is reached, but that's not really the point.
In Lua (as in most programming languages) the term function
really refers to two different things.
In math, a function is something that takes values in, does something, and gives you values back.
For example, the "square root" function takes in a number and spits out the square-root of that number.
In Lua, you use return
to state which value you give out. For example, here is a square
function which returns the square of a number:
function square(x) -- x is the 'in' number return x * x -- `x * x` is the 'out' value end print(square(0)) --> 0 print(square(1)) --> 1 print(square(2)) --> 4 print(square(3)) --> 9 print(square(4)) --> 16 print(square(5)) --> 25
An important part of mathematical functions is that the "out" value depends only on the "in" value. For example, the square(5)
is always going to be 25
-- nothing can change that.
On the other hand, workspace:FindFirstChild("Part")
can give you different objects back -- because it depends on the state of the world. It is not a mathematical function. (See below)
(Also called routines, subroutines, and methods in the context of objects)
In programming, a procedure is a block of code that is packaged together. These do something (called "having side-effects").
For example, this announces a player has joined the game:
function announcePlayer(name) local hint = Instance.new("Hint", workspace) hint.Text = "Welcome " .. name .. "!" wait(5) hint:Destroy() end
Procedures may use return
in order to quit before reaching the end. This pattern is called an early return.
For example, here's a "kill toucher" function that uses early return:
function killToucher(toucher) local human = toucher.Parent and toucher.Parent:FindFirstChild("Humanoid") if not human then return -- quit early, because the rest won't work! end human:TakeDamage(20) end
When you just write return
, you're essentially writing return nil
. Since nil
often represents absence, you're allowed to omit it.