I'm new to LUA and I've watched many videos on this but I still to this day don't understand it. Can anyone explain it to me?
Formally, the definition of the return
keyword is "a statement that tells the program to leave the subroutine and return to the return address". Essentially, if we wish to return a result from a computation or various other scenarios that are concluded from a function, we use this keyword to do so. See this example of it's potential applications:
local numberA = 5 local numberB = 2 local function multiplyNumbers(a,b) return a * b end local result = multiplyNumbers(numberA, numberB)
This will return the multiplication of parameters A and B to wherever this function—or the fancy term "subroutine"—was called, as aforementioned above. To break this down, this is what's occurring:
local result = a * b local result = 10
An easy way to grasp return
, is to see is as the "give back" keyword. Before I finish, let's see another example:
local hello, world = "Hello", "world!" local function concatenateStrings(a,b) return tostring(a..' '..b) end print(concatenateStrings(hello, world)) --> 'Hello world!'