What is the "return" for?

What do you mean? Do you mean, can you use the function info outside the function? If so, you can kind of if you use pre-defined variables.

I think I get it, but how do you return values?

Say you wanted to retrieve values inside a function using math:

local function GetValues(Number, Number2)
    return Number * Number2
end

local Result = GetValues(2, 4)
print(Result)

Since Number & Number2 are our parameters stored in our function, we can return it using a multiplication operator & reference it later on

Then we can get the Result of 2 random values, say 2 & 4

Since we’re multiplying, the result would output back as 8

Number

Number 2

I mean, what are you doing to return the value to this parameter, that is, what the return does is return a value?

I mean, when you put “return number * number 2”

what it does is return the value of those parameters
not?

another thing what is the difference to put “return”

and put “return number * number2”

because in another example you only put the “return”
you explain me

That’s basically what it does

We’re returning the values received by the function

“If your friend asked you for something in another room, you would go get it and bring it back”

The GetValues(2, 4) is what we want to retrieve when we call the function, and since we’re returning it back as a multiplication operation it’ll result back as 8

return also ends the function early, that’s all

1 Like

In programming , return is a statement that instructs a program to leave the subroutine and go back to the return address. … In most programming languages, the return statement is either return or return value, where value is a variable or other information coming back from the subroutine, function.

Could you show me an example because I still don’t understand it and I’m getting stressed?

Return is used to well return information back to the requester, for example:

function add(x, y)
    return x + y
end

print(add(2, 3)) -- this will print 5 as the function returns 5

Another use for return is something called guard clauses. There are basically if statements but shortened.
It turns something like this:

if condition then
    if condition2 then
        if condition3 then
           -- etc
        end
    end
end

Into this which is a lot shorter

if not condition then return end -- if condition is false then stop everything and dont continue
if not condition2 then return end
if not condition3 then return end

both are the same but using these are tidier then lots of if statements

oooo if you wait, explain to me how this works please

if condition then
if condition2 then
if condition3 then
– etc
end
end
end

Basically, it just checks if the conditon is equivalent to true.

For example:

if (1 + 1) == 2 then
   print("1 + 1 is 2")
end
1 Like
if 1== 1 then-- if this is true it will run
    if 3==3 then-- will run if the top if was true
        if 2==2 then-- will run if the top if was true and run what is bellow
           -- etc
        end
    end
end

return is used in functions to return various different values. In this example I have set up below, return is returning an Instance(A part), and a Vector3(A position value). You send information in(If needed), and you send information back(if needed). prt is the Part that is generated by the function. pos, is the Position generated by the function.

function CreatePart()
	local p = Instance.new('Part', workspace)
	p.Anchored = true
	
	return p
end

function FindRandomPosition()
	local r = math.random
	
	local RandomPositionX = r(0,100)
	local RandomPositionY = r(0,100)
	local RandomPositionZ = r(0,100)
	
	local Total = Vector3.new(RandomPositionX,RandomPositionY,RandomPositionZ)
	
	return Total
end



local prt = CreatePart()
local pos = FindRandomPosition()

prt.Position = pos

Getting the values from return is quite simple once you understand it.

If I have a function with one value I would do something like this.

function x()
	local z = Color3.new(0.8,1,0)
	
	return z
end

local q = x()

print('This is a color value.')
print(q)

If I wanted to have more than one value of information sent back I would do something like this.

function a()

local b,v = workspace.Gravity, Vector3.new(23,1,54)

return b,v

end

local a,b = a()

print('This is the current gravity of the workspace')

print(a)

print('This is a Vector3 value also known as a position.')

print(b)

Hope this helps!

Yes, I understand it much better, but I still don’t understand how to exit a function: c

When you say exit, do you mean close off? Like,

function blah()
--stuff
end

Hello, let’s first review what functions are by a non programming example. Hopefully this doesn’t over complicate things.

First, functions can be described as someone else that can help you to complete some task. Let’s say this function is the person you pay to do your homework! This person will ONLY complete your homework IF you have paid the right amount!

So, what happens if you don’t pay the right amount, say $12? Well, they come back with, return with, nothing as you have not fully paid them!

function doHomework(money)
	if money < 12 then
		return nil
	end
end 
doHomework(11) --This returns nil! 

Note: return and return nil are somewhat similar so you can also rewrite it as such. But, returning nil and nothing are different!

function doHomework(money)
	if money < 12 then
		return
	end
end 
doHomework(11) --This returns nothing! 

You can still compare the result in this example to nil. doHomework(11) == nil is true.

So, now, let’s say you do have that $12 available, what happens? They return your completed homework! So, lets expand on the previous example.

function doHomework(money)
	if money < 12 then
		return
	end
	return "Your finished homework!"
end
doHomework(11) --This returns nothing
doHomework(12) --This returns “Your finished homework!”
doHomework(13) --This returns “Your finished homework!” 

Notice how we “exit” the function and the rest does not run if you don’t have greater than or equal to $12?

Yes, that’s what I was looking for: Thank you, I think I understand better

After you complete lines of code in a function, you can send the data back to the called function by using return
Here is roblox’s page on using return in functions

Functions are capable of accepting values from another function. return is a keyword which returns “values” to the caller and exits out of the function.

Example of an closure returning some values to the caller:

-- this script is actually a main function!

local function bo()
      return 5 -- return 5 to whoever called the function
end

print(bo())  -- 5!

return is an extremely useful keyword that is usually used for two purposes:

  • Returning something such as a variable, function etc.
  • To exit a function early

Example:

-- Let's make a simple greet program!
function Greet(Name) 
return "Hello, "..Name.."!" -- Returning the name, with 'Hello' attached to it
end

local Greeting = Greet("dew43rgrt5")
print(Greeting)  -- Prints 'Hello, dew43rgrt5!'

Now, in the above scenario, we could’ve just used a print() in the function itself. However, by using return and saving the Greeting in a variable (named Greeting), we can use the Greeting later and for other purposes.

2nd purpose - using return to exit a function/statement early

Suppose I want a certain condition to be met. I can’t really explain this, so let’s put it into code.

function CheckIfFruit(FruitName)
if FruitName ~= "Apple" then return end -- In this case, when we use return, Lua does not execute any statements beneath it. It 'stops' the function in a way. 
print("Mmmm... "..FruitName.."s! My favorite!")
end

CheckIfFruit("Orange") -- Prints nothing
CheckIfFruit("Apple") -- Prints 'Mmmm... Apples! My favorite!'

I hope this tutorial on how to use return was simple and understandable. If you still need help, please don’t hesitate to reply to this post!