Is recursion a feature in and of itself?

后端 未结 9 493
陌清茗
陌清茗 2021-01-30 05:49

...or is it just a practice?

I\'m asking this because of an argument with my professor: I lost credit for calling a function recursively on the basis that we did not cov

相关标签:
9条回答
  • 2021-01-30 06:43

    There are many point of views to look at regarding the specific question you asked but what I can say is that from the standpoint of learning a language, recursion isn't a feature on its own. If your professor really docked you marks for using a "feature" he hadn't taught yet, that was wrong but like I said, there are other point of views to consider here which actually make the professor being right when deducting points.

    From what I can deduce from your question, using a recursive function to ask for input in case of input failure is not a good practice since every recursive functions' call gets pushed on to the stack. Since this recursion is driven by user input it is possible to have an infinite recursive function and thus resulting in a StackOverflow.

    There is no difference between these 2 examples you mentioned in your question in the sense of what they do (but do differ in other ways)- In both cases, a return address and all method info is being loaded to the stack. In a recursion case, the return address is simply the line right after the method calling (of course its not exactly what you see in the code itself, but rather in the code the compiler created). In Java, C, and Python, recursion is fairly expensive compared to iteration (in general) because it requires the allocation of a new stack frame. Not to mention you can get a stack overflow exception if the input is not valid too many times.

    I believe the professor deducted points since recursion is considered a subject of its own and its unlikely that someone with no programming experience would think of recursion. (Of course it doesn't mean they won't, but it's unlikely).

    IMHO, I think the professor is right by deducting you the points. You could have easily taken the validation part to a different method and use it like this:

    public bool foo() 
    {
      validInput = GetInput();
      while(!validInput)
      {
        MessageBox.Show("Wrong Input, please try again!");
        validInput = GetInput();
      }
      return hasWon(x, y, piece);
    }
    

    If what you did can indeed be solved in that manner then what you did was a bad practice and should be avoided.

    0 讨论(0)
  • 2021-01-30 06:47

    From what I can deduce from your question, using a recursive function to ask for input in case of input failure is not a good practice. Why?

    Because every recursive functions call gets pushed on to the stack. Since this recursion is driven by user input it is possible to have an infinite recursive function and thus resulting in a StackOverflow :-p

    Having a non recursive loop to do this is the way to go.

    0 讨论(0)
  • 2021-01-30 06:47

    Maybe your professor hasn't taught it yet, but it sounds like you're ready to learn the advantages and disadvantages of recursion.

    The main advantage of recursion is that recursive algorithms are often much easier and quicker to write.

    The main disadvantage of recursion is that recursive algorithms can cause stack overflows, since each level of recursion requires an additional stack frame to be added to the stack.

    For production code, where scaling can result in many more levels of recursion in production than in the programmer's unit tests, the disadvantage usually outweighs the advantage, and recursive code is often avoided when practical.

    0 讨论(0)
提交回复
热议问题