Jump to content

Can any function that calls itself be considered recursive?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Bob93

Bob93

    Newbie

  • Members
  • Pip
  • 3 posts
For instance this: (pseudocode)

function pickFromList returns int

{

output: Pick a number from 1 to 10

get input


if number is from 1 to 10

{

return number

}


else

{

output: Your number is not from 1 to 10

return pickFromList

}


}


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It can be, but I would consider that function a great example of when NOT to use it.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Bob93

Bob93

    Newbie

  • Members
  • Pip
  • 3 posts
Why is it a bad idea to use it like this?
Should I use a while loop instead?
Thanks.

#4
josep

josep

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Normally, a recursive function should have a control variable to avoid an infinite loop. In your case, i think you should specify the size of the list and the logic in which you are picking your numbers.
becuase the way it is, it is very possible to run into an infinite loop.

Good Luck!

#5
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 243 posts
It's a bad idea because when you call the method, the system has to add more information to the stack (yes, I see you Functional languages with your tail recursive optimization, but this is true most of the time!) and eventually you'll run out of stack space and your program will crash. A while loop is a much better choice.

#6
Bob93

Bob93

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks for the replies
Would using a while(true) loop be a good idea?

function pickFromList returns int

{

 while (true)

 {

   output: Pick a number from 1 to 10

   get input


   if number is from 1 to 10

   {

   return number

   }


   else

   {

   output: Your number is not from 1 to 10

   }

 }

}


#7
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 243 posts
Yes, using a while loop would be a good idea :)

#8
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts

Bob93 said:

Why is it a bad idea to use it like this?
Should I use a while loop instead?
Thanks.
Your code is an example of Tail Recursion, which occurs when recursive call is the last statement in the function. In that case code can be formally transformed into iterative form without loss of functionality. Even some optimizers do that for you when they encounter the tail recursion.

Recursive call is much more expensive than iteration because complete stack frame must be built, containing all arguments to the method, some register values, local variables, return address... This is not a small structure anyway, and that's why recursive call is slow compared to iteration.

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts

Bob93 said:

Thanks for the replies
Would using a while(true) loop be a good idea?

function pickFromList returns int
{
 while (true)
 {
   output: Pick a number from 1 to 10
   get input

   if number is from 1 to 10
   {
   return number
   }

   else
   {
   output: Your number is not from 1 to 10
   }
 }
}
while (true) is an infinite loop. You want while (number is not from 1 to 10).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog