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

How to use return effectively?

Asked by 8 years ago

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?

0
You're asking "what" to use return for, or are you asking "how"? User#11440 120 — 8y
0
I'm asking what to use it on and how it's used ambu123 5 — 8y
0
return can stop executing a function (like to stop it), for an example... starlebVerse 685 — 8y

2 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago
Edited 7 years ago

Resources for return

https://scriptinghelpers.org/glossary/return

http://wiki.roblox.com/index.php/Function#Using_Return

http://www.lua.org/pil/4.4.html

Response

return does stop executing the containing function when the return statement is reached, but that's not really the point.

What are "functions"?

In Lua (as in most programming languages) the term function really refers to two different things.

Mathematical functions

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)

Procedures

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

1
I'd like to take this explanation further and add that "Procedures" and "Functions" are also sometimes called "Routines", "Subroutines" and "Methods" interchangeably. (Although when we talk about methods we usually think of a function associated to an object in object oriented programming. E.g: "part:Destroy()", in this case 'Destroy()' is a method of 'part'). People today usually tend to use the Link150 1355 — 8y
Ad
Log in to vote
0
Answered by
theCJarmy7 1293 Moderation Voter
8 years ago
Edited by BlueTaslem 7 years ago

Resources for return

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

Response

return does stop executing the containing function when the return statement is reached, but that's not really the point.

What are "functions"?

In Lua (as in most programming languages) the term function really refers to two different things.

Mathematical functions

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)

Procedures

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

0
i am so sorry i don't know what i did BlueTaslem 18071 — 7y

Answer this question