How to learn returning?

Hello there,

I’ve been a bit confused on the return topic on Roblox Scripting. So I do know what return end does; makes the function not continue if it’s called.

But I’m confused on returning certain stuff, such as returning data in a function etc. Where is returning values used in, in just functions or also in other stuff? And what is the purpose of returning as example a piece of data or a string, etc in a function? And what is it exactly used for, how could it be handy to return values/anything, including on how I would use the data/value I have returned.

Thanks for any responses that could help me with this.

2 Likes

Sometimes, you want the function to process something in itself and expect something out of it. Return is the output of the function, after the input(function calling) is performed. Majority of these functions are usually for data and algorithms.

1 Like

Returning is very important and extrememly useful, here’s how it works :

Let’s say you create a function to multiply a number by 2 (just an example)

function multiply(number)
       number = number * 2
end

multiply(3)

Now it does work, it multiplies the number by 2 but… how do you get the result ? Well that’s exactly where you’ll need returning

function multiply(number)
       number = number * 2
       return number
end

local result = multiply(3)
print(result) --> 6

Here i’m assigning a variable to the function, in other words, to what the function will return

So this will become handy when you want to do the same task multiple times but with a different parameter (in this case the number)

Don’t worry i also was confused when i first discovered return but after practising a lot i understood how it worked and started to use it more and more, and today i can’t live without it lol

Hope this helps, good luck with your scripting !

4 Likes

Suppose you were to call a function.

The function does its thing.

But you want to get a value from the result of the function, so you put a return at the end. like this

function Gyatt(skibidi)
return skibidi*skibidi
end
print(Gyatt(5))

The value of skibidi is 5, and the function returns the square of skibidi, which is 25. This gets printed to the output

4 Likes

So if I’m getting it kind of right, returning can be used to use certain data or a value whenever you mention the function?

2 Likes

Return is used mostly for functions, but it can be used to stop loops. You use a function to manipulate data and then return to a variable.

-- Adds two numbers and returns the result
function addNumbers(a, b)
    local result = a + b
    return result
end

-- Usage
local num1 = 5
local num2 = 10

local sum = addNumbers(num1, num2)

print("The sum of", num1, "and", num2, "is:", sum)

3 Likes

Return is a command that returns a new value.For an example:

local function Calculate(Number1,Number2)
    return Number1 + Number2 --Returns the result
end

print(Calculate(5,10)) --prints the result of the problem
4 Likes

Yes, returning allows you to get a result of a function, depending on a starting value

Thanks a lot, including with the other people explaining it.

2 Likes

Returning should not be used to stop a loop, as it will stop any code after it. To stop a loop you should use the word break

1 Like

We’re always here if you need any help you can tag someone that knows about your issues :slight_smile:

Yes, you are right. Unless you want to exit the function.

1 Like

True.the break command is used to stop or break any loops for it.

local function Calculate(Number1,Number2)
    return Number1 + Number2
end

while true do
wait(1)
local Result = Calculate(math.random(1,99),math.random(1,99))
if Result > 50 then
print("The number "..Result.." is bigger than 50 so We are closed for now also cry about it"
break
else
print("The number "..Result.." is smaller than 50 so We are now opened till result is bigger than 50")
end
end

Well I dont see why you should use return. Its just a complicated way. You could just assign a new variable and add to that. It is way easier to understand and deal with and your code is more organized.

local result = nil
function multiply(number)
   number = number * 2
   result = number
end

multiply(3)
print(result) --> 6

Imo the hardest part about learning returning is to find an use for it, I recommend learning about module scripts (and storing functions in them) to really understand how useful returning is.


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

print(add(5 + 5))

Also I don’t think this example (and all the other math ones) is good, it gets the point across, but it doesn’t really show you how useful returning can be.

This was just an example of how it works, as this was the question and the purpose of this post. In other cases when you would need to use that function a large amount of time, you can’t just make 391 variables.

For a good example of something i recently used, i made a function to convert a color3 value into a table (because data store can’t save color3 values).

function toTable(color : Color3)
     return {R = color.R, G = color.B, B = color.B}
end

A very simple function, but it would be very annoying to copy and paste it along the hole script, especially if for some reason i would want to change how it works. With the returning function i can just do that :

(Example)


local house = workspace.House

PlayerData.House.WallColor = toTable(house.wall.Color)
PlayerData.House.FloorColor = toTable(house.floor.Color)
PlayerData.House.RoofColor = toTable(house.roof.Color)

-- etc
1 Like