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

Is it pissiblr to call a function from inside it?

Started by Apfeldstrudel, 26 May 2013 - 03:43 AM
Apfeldstrudel #1
Posted 26 May 2013 - 05:43 AM
for example:

function name()
  name()
end
AndyAndreiH #2
Posted 26 May 2013 - 05:44 AM
Yes, it is.
Bomb Bloke #3
Posted 26 May 2013 - 05:48 AM
Yes, you can do that, but in your particular example it'll quickly (as in, near instantly) overflow and crash. The Lua interpreter needs to keep track of each function call (including each call to the same function), and as written, your function calls never end - they just open up more instances; so Lua's tracking name() calling name() calling name() calling name() etc until it gives up and throws its hands in the air.

So, if you must call functions recursively, make sure that they will eventually stop doing so before things get out of hand. As an example, you might use recursion to help a turtle path find (have a function check what blocks a given block can lead to, then have it call itself a few times to see where those NEXT blocks can lead to, and so on), but if you hit an overflow before it determines its path the program will crash.

The actual upper limit seems to be 256 function calls open at a time, so you've got a lot of room to play with so long as your calls are eventually ending.
theoriginalbit #4
Posted 26 May 2013 - 05:52 AM
As bomb Bloke stated, it is possible. However you shouldn't do a recursive call, well, ever… there are very few circumstances where recursion is actually required over a simple loop. There is a way to stop the stack overflow error that Bomb Bloke stated by using a Tail Call (or Tail Recursion if you prefer), however again, I advise against it and advise you just use a loop, without knowing why you want to do this I'm still going to say that chances are it is your best option to do something like this.

function name()
  -- code
end

while true do
  name()
end
Apfeldstrudel #5
Posted 26 May 2013 - 09:38 AM
OOOhh… i think i typed the question in a bad way- is there any way to… "Restart" the function?
Engineer #6
Posted 26 May 2013 - 09:55 AM
You call this function:

local function foobar()
   print("foobar")
end
With:
foobar()

Then the function starts. If you want to escape a function, you can use return:

local function foobar()
   if something then
	   return
   end
   print("foobar")
end

You can give return arguments like so:

return "It did not work!"
--# and catch the string
local s = foobar()

I think I provided enough info to make it work :P/>
theoriginalbit #7
Posted 26 May 2013 - 09:58 AM
To extend on what Engineer stated, you can make it seem like a function has restarted… Here is a big write up…

An example of a code that calls the function again, only once, we don't want a stack overflow.

local runAgain = true
local function name()
  if runAgain then
	runAgain = false
	name()
  end
  print('Hello')
end
name()

The output
Hello
Hello

What it does
program calls `name`
if statement is true, `name` calls `name`
second `name` prints "Hello"
second `name` finishes
program returns back to first `name`
first `name` prints "Hello"
first `name` finishes
program finishes

So lets say now we actually want the function to "restart", that would mean that it should only print "Hello" once.
Well if we make one change, we can give it the feeling of the function "restarting". This difference is putting the keyword `return` before the second call to `name` like so

local runAgain = true
local function name()
  if runAgain then
	runAgain = false
	return name()
  end
  print('Hello')
end
name()

The output
Hello

What it does
program calls `name`
if statement is true, `name` calls `name`
second `name` prints "Hello"
second `name` finishes
program returns back to first `name`
first `name` returns back to program
program finishes

hope that I understood what you meant correctly, that this makes sense, and that I was able to help. :)/>
Edited on 26 May 2013 - 07:59 AM