Help with understanding function returning

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear! I want to learn Function returning and how it works

  2. What is the issue? Include screenshots / videos if possible! I I still don’t understand function returning after watching a video about returning

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? Yes but they don’t explain it very well i have tried to watch a YouTube video and I’m still unable to understand returning functions

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local function Test(num1,num2)

end

Test(5,6) -- prints 11
Print(Test)
-- how do you return?

Hello i’m having trouble understanding function returning can someone please explain to me how to does this and how function returning works thanks for any help!

1 Like
local function Test(num1,num2)
     local sum = num1 + num2
     return sum
end

local call =Test(5,6) 
print(call) -- 11
print(call + 5) -- 16

Returning in a function, will let you send back data and use it again.

1 Like
local function Test(num1,num2)
     local sum = num1 + num2
     return sum
end

local call =Test(5,6) 


Hope this diagram helps lol

2 Likes

Whenever you define a variable typically you assign it a value.

Local myName = “FangScripting”
Local myFavoriteNumber = 22

A Function can allow you to define your variable after doing something first.

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

local checkMathProblem = Add2Numbers(5, 5)
print(checkMathProblem) – Output of 10

Another example is using a function to return a Boolean (true or false) value. For example, let’s say you were creating a store and wanted to ensure the player had enough Gold to buy an item. You might use code like the following:

local playerGold = 5000
local purchasePrice = 1000

local function checkIfCanPurchase(playerGold)
	if playerGold > purchasePrice then
                -- Player has enough Gold to buy the item
		return true
	else
                 -- Player doesn't have enough gold
		return false
	end
end

if checkIfCanPurchase(playerGold) then
	--Deduct price from player
	playerGold = playerGold - purchasePrice
	
	-- Add item to player inventory
end

Hey there! Adding on to your last example, returns also immediately end the function, so anything after a return will not run.

So your last example code can be reduced to:


local playerGold = 5000
local purchasePrice = 1000

local function checkIfCanPurchase(playerGold)
	if playerGold > purchasePrice then return true end
       
    -- Player doesn't have enough gold
    return false
end

if checkIfCanPurchase(playerGold) then
	--Deduct price from player
	playerGold = playerGold - purchasePrice
	
	-- Add item to player inventory
end

1 Like

When I first started, I really struggled with returning too. But its really simple I promise.

First of all, returning only works with functions (and module scripts but ignore that for now, you can learn about it later) so here is an example of a function:

local function ThisIsAFunction()

end

To keep it simple, returning will do 2 things. Firstly, it will stop the function. example:

local function ThisIsAFunction()
    print("This will print")
    return -- stop function
    print("This wont print")
end

Secondly, it returns something. For example, if you wanted to return the number 2, you would say:

local function ThisIsAFunction()
    return 2
end

You can infact return multiple things (arguments):

local function ThisIsAFunction()
    return 1,2,3,"A","B","C",true,false
end

so what exactly is Returning and how do you use it? its hard to explain so i will use an example.
you know how to create a part with a script?

local Part = Instance.new("Part")

believe it or not, this is a function. craazzyyyyyyyyyy! and it actually returns the part you created to that variable called “Part”. Part.Transparency Part.Parent
ok ok ok. so now *how exactly do i get this returned item from a function?
it is done through a variable (like above)! wooooooowwwww
example:

local function ThisIsAFunction()
    return 2
end
local returnedthingy = ThisIsAFunction()

“OKOKOKWHAT?” you may ask. very suprisingly, saying this: ThisIsAFunction() anywhere in the code, will run the function, just so you know.:happy2:

local function ThisIsAFunction()
    return 2
end
local returnedthingy = ThisIsAFunction()
print(returnedrhingy)

anyways, what do you think this prints?

answer

2

how about this?:

local function ThisIsAFunction()
    return "69"
end
local returnedthingy = ThisIsAFunction()
print(returnedrhingy)
answer

“69”

so you get the point.
B U T - T H E R E S - M O R E . . .
you can return many things.
example:

local function ThisIsAFunction()
    return 1,2,3,4
end
local returnedthingy = ThisIsAFunction()
print(returnedrhingy)

what you think this prints?
hint: its not (1,2,3,4) suprisingly

answer

1

BUT WAIT… WHAT?
…yes, to get multiple returned items / arguments. you actually need multiple variable. like this:

local function ThisIsAFunction()
    return 1,2,3,4
end
local returned1,returned2,returned3,returned4 = ThisIsAFunction()
print(returned1, returned2, returned3, returned4)

wat u think this prints? hint: its 1,3,2,4

answer

sike! I was joking
it prints 1,2,3,4
(don’t doubt yourself if u did)

thats it, there are more complex things u can do with returning (read other comments) but this is to help u understand.
if u dont understand, dont be discouraged, wana know how i understood? ;-; I - d i d n ’ t . . . it took me 2 years to learn it. you will get there :happy3:

4 Likes

Or you can return an array, to get all the values in one variable

1 Like

ye true, but idk if he;s a beginner, soooooo…! yeah
:happy2:

1 Like