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

What is the Return statement used for?

Asked by 4 years ago

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?

0
u can retrieve data when u use return in a function, return also stops loops function f() return 'noob' end print(f()) greatneil80 2647 — 4y

1 answer

Log in to vote
3
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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!'
0
SO basically your using it to return a answer ( as an ex ) NoMarenHere 0 — 4y
Ad

Answer this question