What is the "return" for?

I explain how a few weeks ago I started programming and I have seen several explanations about the “return” but I do not understand what it is for. I do not know how to make me understand that I will only say what the “return” is for

I would appreciate if you could give me an understandable example please: D

edit:

Please provide me with as much documentation as possible. I am overwhelmed by not understanding: c

]

To exit a function? I’m not sure how to explain it differently. I usually see it used when someone wants to exit a function early(before the end is reached otherwise)

if something == true then
     return
end

--do other stuff....

you can also return values to the caller

To simply return a value. Let’s say you have a function that adds five to a number

function addFive(n)
   return n+5
end

local variable = addFive(8)

Variable would equal 13

1 Like

:stuck_out_tongue:

3 Likes

ok so i don’t remember if ‘return’ is for printing/returning a value or its like the OTHER return in which it just is like:

if outcome is not what we wanted it to be:
    return (basically just exits the function)
local function IsNormalMember(Player)
    if Player:GetRoleInGroup(groupId) == “Member” then
   return true
  else
   return false
end

game.Players.PlayerAdded:Connect(function(Player)
  local IsInGroup = IsNormalMember(Player)
  if IsInGroup == true then
       print(Player.Name..” is a member of the group!”)
  end
end)
local function sayHi()
  local printThis = "Hi"
  return printThis
end
-- Print returned value of sayHi
print(sayHi())

return does this

local function add(num1, num2)
 local answer = num1 + num2
 return  answer
end

local NumberStuff = add(1, 2)-- adds numbers gives answer in variable

print(NumberStuff)-- 3

if put the function in a variable the variable is equal to what the functions return

2 Likes

It can be used for exiting functions but also returning a value from the function. Thus making kind of like code which runs to create a variable. Here’s what I mean.

Say you wanted to add to numbers and print their sum. You could create a function


function addNumbers(number1, number2) --declare our function 
       return number1 + number2   --add the numbers
end

local sum = addNumbers(1, 3)
print(sum) -- this would print 4

In this code, what return does, is it exits the function but also gives the function the value number1 + number2. Which is 4

  • So, return can also give a function a value sort of making into a big variable.

I think I get it, but can you run something outside of a function? or as

A return statement returns occasional results from a function or simply finishes a function. There is an implicit return at the end of any function, so you do not need to use one if your function ends naturally, without returning any value.

What do you mean? Do you mean, can you use the function info outside the function? If so, you can kind of if you use pre-defined variables.

I think I get it, but how do you return values?

Say you wanted to retrieve values inside a function using math:

local function GetValues(Number, Number2)
    return Number * Number2
end

local Result = GetValues(2, 4)
print(Result)

Since Number & Number2 are our parameters stored in our function, we can return it using a multiplication operator & reference it later on

Then we can get the Result of 2 random values, say 2 & 4

Since we’re multiplying, the result would output back as 8

Number

Number 2

I mean, what are you doing to return the value to this parameter, that is, what the return does is return a value?

I mean, when you put “return number * number 2”

what it does is return the value of those parameters
not?

another thing what is the difference to put “return”

and put “return number * number2”

because in another example you only put the “return”
you explain me

That’s basically what it does

We’re returning the values received by the function

“If your friend asked you for something in another room, you would go get it and bring it back”

The GetValues(2, 4) is what we want to retrieve when we call the function, and since we’re returning it back as a multiplication operation it’ll result back as 8

return also ends the function early, that’s all

1 Like

In programming , return is a statement that instructs a program to leave the subroutine and go back to the return address. … In most programming languages, the return statement is either return or return value, where value is a variable or other information coming back from the subroutine, function.

Could you show me an example because I still don’t understand it and I’m getting stressed?

Return is used to well return information back to the requester, for example:

function add(x, y)
    return x + y
end

print(add(2, 3)) -- this will print 5 as the function returns 5

Another use for return is something called guard clauses. There are basically if statements but shortened.
It turns something like this:

if condition then
    if condition2 then
        if condition3 then
           -- etc
        end
    end
end

Into this which is a lot shorter

if not condition then return end -- if condition is false then stop everything and dont continue
if not condition2 then return end
if not condition3 then return end

both are the same but using these are tidier then lots of if statements

oooo if you wait, explain to me how this works please

if condition then
if condition2 then
if condition3 then
– etc
end
end
end