So I’ve been trying to figure out returning. So I basically got a simple script made with this:

local function f()
print("f")
return "ReturnSucces"
end
print(f())

The problem is tho, it now prints out “f” and “ReturnSucces” But what is returning really used for since I don’t understand. Why couldn’t I just type print("ReturnSucces’) instead of typing with return. Can anyone explain to me what returning exactly does and what it’s used for?

The “return” statement in programming is used to return a value or a result from a function. In your code, the function “f” returns the string “ReturnSuccess”, which is then printed by the “print” statement outside the function. This allows you to use the value returned by the function in other parts of your code. Simply using “print” instead of “return” would not allow you to access the value from the function outside of it.

Returning is useful if you want to give a function a value, and expect an output from that function.
For example (this is a really basic example, returning is used in much more complex code), lets say we have a variable named myNumber, with the value 3, and we wanted to multiply it by 4, and then divide it by 5.

local function process(number)
return (number*4)/5
end
myNumber = 3
print(process(myNumber))

If you ran this, it’ll output some decimal gibberish.
“Why not just do it all in the print statement?” you say.
It’s because when we’re doing complex work on a variable, it’s way easier to store all that in a function, than to keep copy and pasting that code each time. It makes your code way more maintainable and readable. I apologise in advance for the terrible example.

local function f() -- Defines a local function, and you've named it "f".
print("f") -- You print "f" here in the output
return "ReturnSucces" -- You return the text/string "ReturnSuccess"
end
print(f()) -- Here you print whatever your defined "f-function" returns. In this case you decided to return the text "ReturnSuccess", but it could be anything, here's a sample:
local function Calculate(NumberA, Type, NumberB)
local result
if Type == "plus" then
result = NumberA + NumberB
elseif Type == "divided" then
result = NumberA / NumberB
elseif Type == "minus" then
result = NumberA - NumberB
elseif Type == "multiplied" then
result = NumberA * NumberB
end
return result
end
print( Calculate(5,"plus", 10) )
print( Calculate(5,"divided", 10) )
print( Calculate(5,"minus", 10) )
print( Calculate(5,"multiplied", 10) )

We “return” the result to the prints we have at the end of the script. return just takes whatever “result” we got inside the function, and takes it with us outside the function, this is also why the first print, will print 15 and not "Calculate(5,“plus, 10)”