This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Engineer's profile picture

[LUA][Question] 'RETURN'

Started by Engineer, 30 January 2013 - 08:11 AM
Engineer #1
Posted 30 January 2013 - 09:11 AM
Hello,

As in the title, this topic is about return. My main question is: how to use return? I read up and it has to be in the same place as break, but the function different. And I don't know what the function of this is.

I know break is to simply break out of a loop. This straight forward to me and thats clear.

I see many things like:

return true -- or false

return var1

How can you use this true or false, or even the variable?

I am wondering how this exactly works, and thank you in advance.

Greetz,

Engineer
remiX #2
Posted 30 January 2013 - 09:37 AM
Well, like simple turtle functions:
turtle.forward() if it goes forward, it returns true, if it doesn't (block in the way / no fuel) it will return false.

And you can return variables from tables etc, for like iterating through a table and if a condition is met it will return a variable from that index of the table.


tab = {
{txt = "Pastry", object = "pie"},
{txt = "Juicy", object = "apple"},
{txt = "Wheat", object = "bread"}
}

function checkObjectOfTxt(_table, text)
    for i = 1, #tab do
        if tab[i].txt == text then
            return true, tab[i].object
        end
    end
    -- If nothing was find, return false, nil
    return false, nil
end

b, ob = checkObjectOfTxt(tab, "Wheat")
if b then
    print("Yay! Wheat has an object of " .. ob)
end

Things like that…
Engineer #3
Posted 30 January 2013 - 11:08 AM
Well, like simple turtle functions:
turtle.forward() if it goes forward, it returns true, if it doesn't (block in the way / no fuel) it will return false.

And you can return variables from tables etc, for like iterating through a table and if a condition is met it will return a variable from that index of the table.


tab = {
{txt = "Pastry", object = "pie"},
{txt = "Juicy", object = "apple"},
{txt = "Wheat", object = "bread"}
}

function checkObjectOfTxt(_table, text)
    for i = 1, #tab do
        if tab[i].txt == text then
            return true, tab[i].object
        end
    end
    -- If nothing was find, return false, nil
    return false, nil
end

b, ob = checkObjectOfTxt(tab, "Wheat")
if b then
    print("Yay! Wheat has an object of " .. ob)
end

Things like that…

So to be clear, that return false, nil does actually shutdown the program?
ChunLing #4
Posted 30 January 2013 - 11:23 AM
No, it exits a function, providing the following values as return values.

If used in the main body of a program, outside of any function definition, it exits the program (because the program is run as a function by shell). This can be useful. But mainly you want to use it inside of a function definition so that the function will return a value (or values) you can use.
theoriginalbit #5
Posted 30 January 2013 - 11:44 AM
think of it like this, every time you call a new function it goes into a 'stack'. Then when your working along in a function and you want to exit early, lets say that something happened and there is no point doing the rest, you can use return. return acts by 'returning' to the previous function that called it and if there is no function that called it (i.e. its the main code body) it will then exit the program.

So a visual aid (NOTE: its not 100% accurate, its for simplicities sake to explain):
Spoilerhere we have the main code body
body

now we call the enterBuilding function
body —> enterBuilding

now we call the hello function from inside the enterBuilding function
body —> enterBuilding —> hello

now the hello function finishes, its done all it needs
body —> enterBuilding

now we call the leave function from inside the enterBuilding function
body —> enterBuilding —> leave

but oh no, we were actually outside all the time, there is no need to leave, so we return from leave
body —> enterBuilding

now we return from enterBuilding
body

and now we call runAway from body
body —> runAway

we run out of energy and leave runAway
body

program finishes
empty
Engineer #6
Posted 30 January 2013 - 12:21 PM
Thanks to all of you. I now got a idea by return, but have to see some code again now I know the definition. This is really really appreciated and thank you cant be enough said.

Thank you :D/>
tesla1889 #7
Posted 30 January 2013 - 03:22 PM

local fn = function()
return "this string is to be returned"
end
local s = fn()

s now equals the string "this string is to be returned"
ChunLing #8
Posted 30 January 2013 - 03:39 PM
You can also use conditionals to have alternate returns or return variables.
function isStringTrue(inputstring)
  if inputstring == "true" then
    return true
  end
return false end
print(isStringTrue("any old string")) --prints false
print(isStringTrue("true")) --prints true
Note that, if the inputstring == "true", the first return (of true) is reached and the function exits without reaching the second return.
function outputInput(anyinput)
return anyinput end
s = outputInput("any input") --s now has the value "any input"
m = outputInput(s..s) -- m now has the value "any inputany input"
s = outputInput(tonumber(m)) --s now has the value nil
This totally useless function simply returns the argument you used in your function call.
remiX #9
Posted 30 January 2013 - 06:00 PM
You can also use conditionals to have alternate returns or return variables.
function isStringTrue(inputstring)
  if inputstring == "true" then
	return true
  end
return false end
You can shorten that…

function isStringTrue(inputstring)
    return inputstring == "true"
end

:D/>
Engineer #10
Posted 30 January 2013 - 08:15 PM
Okay, I know how to use return now. Thaak you all! You guys cant be thanked enough! Thank you!
ChunLing #11
Posted 31 January 2013 - 02:46 AM
Yes, you can also return an expression (or rather, the value to which the expression evaluates).