9

I am trying to figure out what you call a function that references itself. Is this termed recursion? Or is it simply a self-referencing function?

user858642
  • 187
  • 2
  • 2
  • 6
  • If it never *calls* itself then I'd argue it's just silly code ;-) Otherwise, see the answers. A recursive function is one that *calls* itself. Direct recursion is the *act* of *calling* itself. Therefore, if nothing is ever *called* (but the function, say an anonymous function, still has a reference to itself) then... ;-) –  Jul 26 '11 at 18:11

3 Answers3

11

It is a recursive function. Direct recursion is when a function calls itself.

ssell
  • 6,429
  • 2
  • 34
  • 49
2

A function that calls itself is, as you suspect, called "recursive".

Patrick87
  • 27,682
  • 3
  • 38
  • 73
0

Recursive or self recursive is what I usually refer to it as. Just be careful so you don't get stuck in a loop calling yourself, eventually blowing the stack.

Also keep in mind your variable's scope. Declare variables as static if they are needed to be shared throughout all recursion levels (or declare them outside the function). Pass variables to the function if you need to specific information passed from one level to the next. And finally, use local variables in the function that are needed to keep state for a current level of recursion. Local variables will make a copy on the stack for each recursion level you call, and pop back to the previous values for each recursion that unwinds.

Brain2000
  • 4,655
  • 2
  • 27
  • 35
  • I think the OP was just fishing for a homework / online test question answer :) – Rag Jul 26 '11 at 18:18