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

How can you effectively use returns?

Asked by 4 years ago

I'm having trouble what returns are and how you can even use them. They seem very useless but it's a big part of scripting. What are returns and how can I use them effectively?

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Not sure what you mean by returns not being useful. A huge part of function creation for the purpose of abstraction involves returning values from said functions. Let's use an example and say that the Roblox library doesn't have the table.find() function for finding an instance of a value in a table.

We have a table set up as follows: local myTable = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"} and we want to know whether orange is in the table or not. Normally we would use table.find(myTable, "orange") and would be returned its position, but in this scenario it doesn't exist so we create our own function defined as follows:

local function inTable(table, value) --table arg is our table, value is what we are searching for
    for i, item in pairs(table) do --setting up a loop to iterate through the table
        if item == value then --checking the value at the position of the table against what we are looking for
            return i --return the table position where the value was found
        end
    end
    return nil --if the item was in the table this line won't be reached, but if it isn't the function returns nil, like table.find()
end

Now we can call this function like this local positionInTable = inTable(myTable, "orange") and we will be returned 2.

This is useful so that you aren't writing the loop inside the function every time, and are instead able to call a function and be returned a value.

Hopefully this wasn't confusing or worded too poorly and that I could help a little with understanding.

0
That looks very confusing. Have anything answer simpler? I'm a beginner scripter and I don't really know about for statements. I don't even know about parameters yet. ItsNocturnal 0 — 4y
0
Once you start moving towards abstraction and exiting inner blocks return will be much more useful. Return is generally used to return a value when a function is called and will exit loops and functions, break only exits without returning something. See here https://www.lua.org/pil/4.4.html msuperson24 69 — 4y
Ad
Log in to vote
0
Answered by
DollorLua 235 Moderation Voter
4 years ago

Returns can be useful in many cases. Giving a value back from a function is an example. In this function "return" is used to return an addition problem:

function add(a, b)
    return a + b
end

local answer = add(1, 2)
print(answer) -- prints 3

It can also be helpful for modules to return information to the caller (Same way as a function is).

Return is a way of sending data back to the original caller as a variable. A default roblox lua example is:

local runService = game:GetService("RunService") -- returns run service

Roblox lua uses return to send back the service run service. There are many uses for return and can be very helpful. Open source functions, modules, etc that give back data should use return so users don't/wont get confused on why it wont work. I mainly use return so I can easily process something and get info back from functions, as functions were made to not do repetitive coding.

Answer this question