Jump to content




[LUA][Question] 'RETURN'


  • You cannot reply to this topic
10 replies to this topic

#1 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

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

#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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

#3 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 30 January 2013 - 11:08 AM

View PostremiX, on 30 January 2013 - 09:37 AM, said:

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?

#4 ChunLing

  • Members
  • 2,027 posts

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.

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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):
Spoiler


#6 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

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

#7 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

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"

#8 ChunLing

  • Members
  • 2,027 posts

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.

#9 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 30 January 2013 - 06:00 PM

View PostChunLing, on 30 January 2013 - 03:39 PM, said:

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

#10 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

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!

#11 ChunLing

  • Members
  • 2,027 posts

Posted 31 January 2013 - 02:46 AM

Yes, you can also return an expression (or rather, the value to which the expression evaluates).





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users