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....
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())
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
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.
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.
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
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.
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