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

can you call a function inside itself?

Started by subzero22, 18 January 2014 - 11:43 PM
subzero22 #1
Posted 19 January 2014 - 12:43 AM
I know you can call functions inside other functions. But can you call a function inside itself?

I would test this but right now at work and decided to edit one of my codes. Here's a snipit of my code so you know what I'm trying to ask.


function check()
  if s == 16 and turtle.getItemCount(16) >= 1 then
    print("Turtle ran out of resources to build with")
    print("Please refill turtle with needed blocks and")
    print("press any key to continue building")
    os.pullEven("key")
    s = 1
  end
  if turtle.compareTo(16) == false then
    s = s + 1
    turtle.select(s)
    check()
  end
end
Lyqyd #2
Posted 19 January 2014 - 01:29 AM
That's known as recursion. It's a useful tool for certain things, but if you just want code to keep looping, use an actual loop.