Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: Resource for understanding recursion

  1. #11
    Join Date
    Jul 2008
    Beans
    1,491

    Re: Resource for understanding recursion

    Quote Originally Posted by ps2key View Post
    Now I see that if the conditional if: statement is true (see code), it returns 1 to the last recursion but the value of the parameter in the if:condition fork doesn't get passed along to the else: fork because the if:condition fork ended before doing so. Therefore "n" is still = 2 from the previous recursion. Am I correct? Thank you in advance.
    No. Consider the various states of the program I wrote down earlier (mentally unwinding the function calls):

    We have a function call print: in this case it is passed an *integer* argument. What is the value of the integer argument? factorial(3). But print() never knows that it receives `factorial(3)' as argument: all it will ever encounter is the value (6). At the moment we are `inside print' both `this specific call to factorial' and `3' are history: no longer accessible to the pogram. This is because the arguments to a function a resolved *before* the function receives them.

    When you apply the same reasoning to the non-base case factorial(2) we end up with the value `2 * factorial(1)'. `factorial(1)' is at that point not yet resolved, so we kick off a *new* call to `factorial' with argument 1 to find out. When we are in factorial(1) we do not know anything about any prior factorial call: that is _history_. Inaccessible, locked off. You cannot therefore modify something in factorial(2) when inside factorial(1) because you don't know (and never could be sure as one might call factorial(1) from elsewhere) _that_ you came from factorial(2) (and you don't have any means of accessing that information or that function call).

    On the upside of no-outside-world-information: that means you need never worry about what the variable n means to something _outside_ of your _current_ call to factorial: this is encapsulation (locking away) at its finest.

    Note that *yes* there are (sometimes useful) ways around that: it is possible for recursive functions to modify a global datastructure (think about a sorting algorithm of a tree structure) in imperative languages. (But not in pure functional languages which forbid that kind of side effect so in those cases you would return a new data structure.)

  2. #12
    Join Date
    May 2008
    Beans
    5

    Re: Resource for understanding recursion

    Thank you very much Reiger. It makes perfect sense now.

    Also thanks to Paul Miller for his post just prior to mine. I reread it and picked up on this;

    "Once that happens, all the recursive calls in the chain will start to return, and you'll get your answer after all n of them return."
    Last edited by ps2key; June 9th, 2009 at 05:21 PM. Reason: Spelling

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •