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

(probably a difficult question) functions and return statements

Started by Crucidal, 15 February 2015 - 03:11 PM
Crucidal #1
Posted 15 February 2015 - 04:11 PM
Hello,

I ran into a bug with some code (shown below). I managed to solve it but I don't understand why.
Note: The error was that turn(1,"left") could not be resolved (it was nil)

Apparently lua doesn't like muliple return statements in one function?
My google-skills weren't good enough to search as to why this is x)

Does anyone know?

Code containing the bug:


local function turn(amount, side)
  if side == "left" then
	return turtle.turnLeft(amount)
  elseif side == "right" then
	return turtle.turnRight(amount)
  else
	screen.sprint("Function turn was called but no valid side was provided!")
	return false
  end
end

turn(1,"left")

fixed code:

local function turn(amount, side)
  success = false
  if side == "left" then
	  success = turtle.turnLeft(amount)
	elseif side == "right" then
	  success =turtle.turnRight(amount)
	else
	  screen.sprint("Function turn was called but no valid side was provided!")

  end
  return success
end

turn(1,"left")
Edited on 15 February 2015 - 03:26 PM
SquidDev #2
Posted 15 February 2015 - 04:16 PM
You've put turns instead of turn in the function definition? Lua works fine with multiple return statements, so I presume it is the spelling that is the problem, I can't see anything obviously wrong.
Crucidal #3
Posted 15 February 2015 - 04:28 PM
You've put turns instead of turn in the function definition? Lua works fine with multiple return statements, so I presume it is the spelling that is the problem, I can't see anything obviously wrong.

nice catch but that was just me making a typo when I rewrote the code on the forum for readability :-)

You've put turns instead of turn in the function definition? Lua works fine with multiple return statements, so I presume it is the spelling that is the problem, I can't see anything obviously wrong.

nice catch but that was just me making a typo when I rewrote the code on the forum for readability :-)

oh derp. I take that back.

:rolleyes:/> :rolleyes:/> :rolleyes:/> :rolleyes:/> :rolleyes:/> :rolleyes:/>
I should have seen that… XD
InDieTasten #4
Posted 15 February 2015 - 05:12 PM
For interest: Lua is one of the few languages that does true multi-value-returns and therefor also passing and assigning.
Most other languages "implement" this by delegating them into tables and back.
Bomb Bloke #5
Posted 15 February 2015 - 09:09 PM
It's worth bearing in mind that turtle.turnWhatever() doesn't accept a parameter. I mean, you can hand it one, but it'll simply ignore it. You're wanting a "for" loop.
Crucidal #6
Posted 21 February 2015 - 02:56 PM
It's worth bearing in mind that turtle.turnWhatever() doesn't accept a parameter. I mean, you can hand it one, but it'll simply ignore it. You're wanting a "for" loop.

yeaaaaa……I noticed… ^^"
thanks
ElvishJerricco #7
Posted 21 February 2015 - 03:19 PM
For interest: Lua is one of the few languages that does true multi-value-returns and therefor also passing and assigning.
Most other languages "implement" this by delegating them into tables and back.

Even Lua! Under the hood it's implemented by allocating arrays.
Bomb Bloke #8
Posted 21 February 2015 - 11:16 PM
Yes, but that's "under the hood" - when coding in Java, for eg, if you want multiple return values you need to shove them into an array or suitable object yourself.