Forum Moderators: open

Message Too Old, No Replies

Recursive function apparently violates if curly brace behavior.

Please explain why?

         

MarcMiller

11:31 pm on Oct 4, 2008 (gmt 0)

10+ Year Member



Hello
I have been reading the book "javascript: The Good Parts" and have found some example code that confuses me. The code gives you the solution to the Hanoi puzzle. It uses a recursive function and an if statement within the recursive function. If the if statement is true the function calls it self and most of the curly brace body of the if statement is ignored. However if the if statement fails it appears you execute statements within the body of the if statement curly braces in Spite of the fact that the if statement fails. I am saying this because I have checked out this code by both stepping through it with the Firerbug Firefox extension and using the alert statement approach and both seem to yield the fact that when the if statement fails instead of bypassing the entire code within the curly braces you execute the next statement after the recursive call. I am confused. Could you help? The code I am talking about is below.


var hanoi = function (disc, src, aux, dst) {
if (disc > 0) {
hanoi(disc - 1, src, dst, aux);
document.writeln('Move disc ' + disc +
' from ' + src + ' to ' + dst);
hanoi(disc - 1, aux, src, dst);
}
}

hanoi(3, 'Src', 'Aux', 'Dst');

daveVk

1:24 am on Oct 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The function calls itself twice, pre and post writeln. Resulting sequence of calls( as the function does nothing if disc = 0, I have not shown these).

initial entry disc = 3
-- pre entry disc = 2
---- pre entry disc = 1
---- writeln disc = 1
---- exit disc = 1
-- writeln disc = 2
---- post entry disc = 1
---- writeln disc = 1
---- exit disc = 1
-- exit disc = 2
writeln disc = 3
-- post entry disc = 2
---- pre entry disc = 1
---- writeln disc = 1
---- exit disc = 1
-- writeln disc = 2
---- post entry disc = 1
---- writeln disc = 1
---- exit disc = 1
-- exit disc = 2
exit disc = 3

MarcMiller

3:13 am on Oct 5, 2008 (gmt 0)

10+ Year Member



I can see two calls of the function to itself within the curly braces of the if statement. But my question is how can it ever get to the second call of the if statement inside the curly braces since if it passes the if test the function will call itself again before it ever reaches the second call in the if statement. This is what is confusing me. How does it execute anything past calling itself within the curly braces since if the if test fails it will never get into any statements within the curly braces and if it passes then it would immediately call itself stopping all execution of curly brace code because of this call to itself thereby never reaching the second call to itself inside the curly braces.

Fotiman

4:32 am on Oct 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When a function calls another function, it does not stop executing. For example, if I do this:

function foo() { 
bar();
alert('hello');
}

Then when foo is called, it will execute bar and then alert 'hello'. It does not stop processing because it called another function. Your example is identical, except that the function it calls happens to be the same function currently executing. The result is that you end up recursing and then walking back down the tree when you're done.

MarcMiller

5:17 am on Oct 5, 2008 (gmt 0)

10+ Year Member



Does that mean in my example the code recursives and recursives on till the if statement fails and does not continue executing beneath the if statement because of the first failed if statement but rather because of the previous past if statement. Would it be clearer, correct and desirable to use some sort of else continue statement after the recursive call.

daveVk

7:57 am on Oct 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



my question is how can it ever get to the second call of the if statement inside the curly braces since if it passes the if test the function will call itself again before it ever reaches the second call in the if statement

There is nothing special about a function calling itself.

When the first call completes the writeln and second call will happen.