Question on returning

Local function hi()
	print("hi there")
	return "something"
end

local returning = hi()
print(returning)

I don’t get how returning is useful. If someone can tell me, thanks in advance.

The return statement is extremely useful as it serves several purposes, like making module scripts work or getting a value from a function.

What the return statement does is in the name, it returns something i.e. a value. Let’s say for example you have a function which gets all the players in the game, and you want to put them all into a table. It would look something like this:

local function getPlayers() -- Declaring the function
   local players = {} -- Making the table to store all the players names
   for _, v in pairs(game.Players:GetPlayers()) -- Looping through all the players in the game  
     table.insert(players, v.Name) -- Puts the players name into the end of the table
   end
end

It should look something like this. But, there’s no way of us getting the players from this function, all its done is worked it out and ended. We can change that by adding the return to the end of it, which should look something like this:

return players

Now, if it’s called and stored, it will be stored as all the players names, not a nil value.

In short, return is what you give back to whatever is requesting it. If I were a baker, and a customer wanted a baguette, they would request for it and I’d return it to them.

Thank you so much. I didn’t really know much about it. This helps me understand the concept better.

Using return is useful for OOP (Object Orientated Programming), for example:

local mt = {
	__index = {
		changeType = function(t, s)
			t.Species = s
		end
	}
}

local function createObject(name, species)
	local t = {
		Name = name,
		Species = species
	}
	
	setmetatable(t, mt)
	return t
end

local kermit = createObject("Kermit", "Frog")
local missPiggy = createObject("Miss Piggy", "Piggy")

print(kermit.Name)
print(kermit.Species)
print(missPiggy.Name)
print(missPiggy.Species) --> Oh no, piggy is a popular Roblox game!

missPiggy:changeType("Pig") --> Change the Type for missPiggy

print(missPiggy.Species)

Using return, ‘Objects’ (or tables) that use similar functions (methods) can be quickly constructed without requiring an excessive repeat of code - adhering to the DRY principle.

Here’s another example of how it could be useful
Let’s say you have a table, and you want to duplicate it. You could do this:

local function DeepCopy(tabl)
    local copytabl = {}
    for i,v in pairs (tabl) do
        If type(v) == "table" then
            v = DeepCopy(v)
        end
        copytabl[i] = v
    end
    return copytabl
end

local initialtable = {1,2,3,4}
local copy = DeepCopy(initialtable)

You use return to assign any value you want to the place you called the function from. Every time you make a variable, you can think of it as this:

local number = 5--what your familiar with

local function getfive()
    return 5
end
local number = getfive() --same thing