#Understanding return statements

1 messages · Page 1 of 1 (latest)

minor bane
#

Guys im trying to learn return statements but im having a difficult time understanding even the slighest bit of it like what it is or what it does and why its used can someone help out? this is the code i saw in the video but i honestly dont get it
''''lua
local function addition(number1, number2)
local result = number1 + number2
return result
end

local printResult = addition(8, 2)

surreal knot
#

you can simplify this

#

imagine talking with another person and you ask him how his day was

now the first scenario: (return) he tells you: it was good
now you have information to use

now the second scenario: he doenst answer
now you will just go on

#

with return

local function addition(number1, number2)
    local result = number1 + number2
    return result
end

local printResult = addition(8, 2)

without return

local function addition(number1, number2)
    local result = number1 + number2
end

local printResult = addition(8, 2) --this is nil

so you use returns if you need the information from where you called a function and the return could be stored in a variable (printResult)

elder lantern
#

Like for example

#
local function makearandomnumber()

   math.random(1,1000)

end

makearandomnumber()

How would you even obtain or get the random number that is created? Thats where return comes in. You can do local number = makearandomnumber() ez pz

surreal knot
#

i think this is gonna confuse him

#

if hes doing simple additions