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

[Lua][Question] Fun Fun Functions lol

Started by TechManDylan, 28 March 2013 - 10:28 AM
TechManDylan #1
Posted 28 March 2013 - 11:28 AM
So i have got a function


function placeBlock()
		turtle.placeDown()
		turtle.forward()

now what I was wondering is how I could pass an argument to it like placeBlock(5). How would I go about doing this?

Edit: Maybe it would help if I listed my full code

if turtle.getFuelLevel() < 1000 then
	    turtle.select(16)
	    turtle.refuel(64)
end
function placeBlock()
	    turtle.placeDown()
	    turtle.forward()
end
function Corner()
	    turtle.turnRight()
	    turtle.forward()
	    turtle.turnLeft()
  placeBlock(2)
  turtle.turnRight()
	    turtle.forward()
	    turtle.turnLeft()
  placeBlock(1)
  turtle.turnRight()
  turtle.forward()
end
function buildCylinder()
  placeBlock(5)
  Corner()
  placeBlock(5)
  Corner()
  placeBlock(5)
  Corner()
  placeBlock(5)
  Corner()
end
buildCylinder()
Engineer #2
Posted 28 March 2013 - 11:58 AM
First of all you want to use a for loop. You do this:

for i = startnumber, endnumber, step do
   --stuff
end 

Step is by default 1

function placedown( times )
   for i = 1, times do
       -- stuff
    end
end

If you have further questiona.. Ask! ;)/>
Mtdj2 #3
Posted 28 March 2013 - 12:13 PM
You might also want to make the function work without an argument:

function myFunc(herp)
for h=1,herp or 1 do
–le stuffs…
end
end

That code will not bug out on you if you forgot a 1 as an argument.

Posted from shitty browser on shitty device…