3

Recently, our teacher gave us a quiz on JavaScript. I'm relatively advanced in terms of programming, so when I came upon a question:

Using the name of a function within that same function will result in an infinite loop?

I answered false because of recursion. From my understanding, you have to use the name of a function in order to call it, so recursion would make this not always true.

Am I correct in my understanding, or is the wording different?

Satpal
  • 132,252
  • 13
  • 159
  • 168
AniSkywalker
  • 449
  • 5
  • 20
  • 2
    It's definitely not always true. It depends on the code in the function, and as you said the possibility of recursion. – j08691 Feb 19 '16 at 18:29
  • The correct answer is "it depends." If there's no sane terminating condition and you're actually invoking that function, not just assigning it to some variable, then yes it will be an infinite loop. – Mike Cluck Feb 19 '16 at 18:30
  • I personally think it largely depends on what "the name of a function" actually means. As long as the same function does not recursively invoke itself with no criteria it is not true, else it is true, but again, it largely depends on what your teacher meant, I'm pretending to say that the question was unclear if the teacher is asking you to answer either true or false. – briosheje Feb 19 '16 at 18:33
  • @briosheje I completely agree. The context of the question made it seem like true meant it always holds, and false is anything else. – AniSkywalker Feb 19 '16 at 18:50

5 Answers5

5

This will result in an infinite, recursive "loop" (a stack overflow):

function callMe() {
    callMe();
}

Whereas this will not result in an infinite loop:

function callMe() {
    if (false) {
        callMe();
    }
}

But both snippits are have a function that "uses" the name of that function (to use the wording of your question).

So, the statement:

Using the name of a function within that same function will result in an infinite loop?

really depends on the logic inside the function being invoked (conditional statements, etc).

Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
  • Only if the implementation provides tail call optimisation - otherwise you'll get a stack overflow. – Ben Feb 19 '16 at 18:31
  • @Ben what optimisation tail call optimisation would do in that case? Never run the loop in the first case? – acdcjunior Feb 19 '16 at 19:08
  • @acdcjunior tail call optimisation will prevent each recursive call from creating a new stack frame, allowing the recursion to continue forever - see http://stackoverflow.com/questions/310974/what-is-tail-call-optimization – Ben Feb 19 '16 at 19:11
  • @Ben I see. I was just wondering if it'd do something other than the infinite loop (which, as you said, it won't). Thanks! – acdcjunior Feb 19 '16 at 19:20
1

It depends on how you are using it.

The statement

Using the name of a function within that same function will result in an infinite loop?

clearly doesn't suggest, How you are using? as in the following example it will not cause infinite loop. The answer should be completely based on the How you are using it?

function callMe() {
    var a = callMe;
}
callMe();
Satpal
  • 132,252
  • 13
  • 159
  • 168
0

As far as I know, you are correct. It is possible to use the name of a function within itself without causing an infinite loop because of some recursion circumstances.

Jrud
  • 1,004
  • 9
  • 25
0

If he's referring to calling the same funciton inside that function, then it's recursion and it could cause and infinite loop or not, it depends on how the function is coded and how it is invoked (if you are invoking it with the same arguments then it might be an infinite loop if there aren't any exit conditions).

Francisco Goldenstein
  • 13,299
  • 7
  • 58
  • 74
0

It does result in an infinite loop if there is no stopping condition that exits the method with something else than a call to itself. So if you disregard the implementation, than your teacher is generally correct... in an academic sense.

alumarcu
  • 133
  • 6