Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone help me understand what is returning, a good example?

Asked by 4 years ago

I see returning a lot especially in functions, what does it do? Is it useful? Any explanation is appreciated.

2 answers

Log in to vote
1
Answered by 4 years ago

Returning is used to send informations back to where the function is called, here is an example:

local function sayHi()
    return "Hi!"
end
-- Print returned value of sayHi
local returnedValue = sayHi()
print(returnedValue)

Here is the expected output:

Hi!

You can also return using integers, as well as performing arithmetics.

local function multiply(x, y)
    return x*y
end

local returnedValue = multiply(5, 5)
print(returnedValue)

25

Returning is very simple and easy to learn and very useful.

You don't have to wrap in a variable print(multiply(5, 5))

Quick Recap: Returning is used to send informations to where the function is called, so whatever your returned value is, that value will get sent.

0
Ah I get it! Thanks for help my friend! GC_Pogi54 27 — 4y
Ad
Log in to vote
2
Answered by 4 years ago

There's a really good Lua documentation page for exactly what you're looking for. https://www.lua.org/pil/4.4.html

0
Thanks for help though GC_Pogi54 27 — 4y

Answer this question