Can you tell us from where you got this advice? I taught programming for many years but I'd never encountered anything like that.

If you look below, you guys just proven me right:

LanX wrote,

"Please note that Perl has a default warning if the recursion level exceeds 100 (it's configurable somewhere)"

Bingo. In this case. If you have tested the OP code, you would have noticed that he has essentially created a loop that counts from 0 to 4 million using a function that calls itself. And as expected, I got the warning from Perl saying that recursion level was exceeded. Then it crashes. In other words, even after correcting all the syntax errors, the program still doesn't run as expected. It crashes. The code could have been written as a for loop: for (my $i = 0; $i < 4000000; $i += 2) { ... } but nah, that's the wrong answer, because harangzsolt said it. When LanX and Cavac write the same thing, it's the right answer. When I write it, it's the wrong answer. Amazing how that works...!

LanX wrote:

"It's true that every recursion can be written with a loop (and more importantly vice versa!)."

And Stevieb wrote:

A design flaw would be intentionally implementing recursion, but incorrectly (which most of the time crashes due to infinite recursion).

Exactly. And In case of Perl, an infinite recursion will be caught and a warning will appear, which is nice. There's a reason for that warning. It warns you that you might want to redesign or rewrite your code! ;-)

Stevieb:

There are very valid uses for recursion, and it works wonderfully when the proper checks and balances are put into place.

I agree. Most of the times, however, recursion, especially when you're studying codes written by beginner programmers, is a flaw, not some profound design feature! And in this case, the OP's code is flawed. That's what we're talking about here.

Cavac wrote,

"You are using recursion. Unless you really know what you are doing (and set strict recursion limits), that's something that can backfire quite easily. (And this isn't a problem that requires recursion)."

Yes, that's basically the same thing that I said.


In reply to Re^5: How am i doing? by harangzsolt33
in thread How am i doing? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.