Recursive question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mdh

    #1

    Recursive question.

    This little program, which is heavily adapted from K&R (Exc 4-12)
    converts an integer to a string, recursively. My questions are these.

    1) I think that as each successive recursion occurs, the automatic
    variables are retained somewhere? so that when the function is
    completed at a later point ( I hope I am expressing this vaguely
    correctly) those automatic variables that were retained are again used.
    Is my understanding correct?

    2) In this particular function, I noticed that as each recursion
    completes it's evaluation, 'k' is returned. I know where 'k' goes in
    the last evaluation, (back to "main") but where does it go, for lack of
    a better phrase, prior to that?

    Thanks in advance.



    /*****/
    # include <stdio.h>
    # define MAXNUM 50

    int main () {
    int itoa( char s[], int i);
    int i=-234, j;
    char s[MAXNUM];
    j=itoa(s,i);
    printf ("the number of characters in %s is %d", s, j);
    return 0;
    }


    /******** itoa ***********/

    # include <math.h>

    int itoa( char s[MAXNUM], int i){
    static int k;
    {
    if ( i / 10)
    itoa(s, i/10);
    else{
    k=0;
    if (i < 0)
    s[k++]='-';
    }
    s[k++]=(abs(i) % 10 + '0');
    s[k]='\0';
    }

    return k;

    }

    /******/

  • Kurt Weber

    #2
    Re: Recursive question.

    mdh wrote:
    This little program, which is heavily adapted from K&R (Exc 4-12)
    converts an integer to a string, recursively. My questions are these.
    >
    1) I think that as each successive recursion occurs, the automatic
    variables are retained somewhere? so that when the function is
    completed at a later point ( I hope I am expressing this vaguely
    correctly) those automatic variables that were retained are again used.
    Is my understanding correct?
    If I understand your explanation of your understanding correctly, then yes,
    your understanding is correct.

    Of course, it could be that I misunderstand your understanding.

    :D
    >
    2) In this particular function, I noticed that as each recursion
    completes it's evaluation, 'k' is returned. I know where 'k' goes in
    the last evaluation, (back to "main") but where does it go, for lack of
    a better phrase, prior to that?
    It doesn't really "go" anywhere...it's ignored, because it's not needed
    except when called from main(). The reason for this is that "k" is static,
    which means that its lifetime is equal to the lifetime of the program,
    rather than the lifetime of the particular instance of the particular
    function call.

    Think of a static local as essentially a global with restricted
    scope--instead of being visible to the entire program or entire module,
    it's only visible to the particular function it's called in. But, its
    value is kept from one call of that function to the next--even if the
    function is called recursively.

    Here's an example:
    int example(void){
    int k = 0;

    k++;
    return k;
    }

    In this example, k will be reinitialized each time example() is called, so
    example() will always return 1;

    int example2(void){
    static int k = 0;

    k++;
    return k;
    }

    In this example, k will only be initialized the first time example2() is
    called, so the return value of the function will increase by 1 each time it
    is called.



    --
    Kurt Weber
    Help stop socialism: Vote Libertarian!

    Comment

    • Eric Sosman

      #3
      Re: Recursive question.

      mdh wrote:
      This little program, which is heavily adapted from K&R (Exc 4-12)
      converts an integer to a string, recursively. My questions are these.
      >
      1) I think that as each successive recursion occurs, the automatic
      variables are retained somewhere? so that when the function is
      completed at a later point ( I hope I am expressing this vaguely
      correctly) those automatic variables that were retained are again used.
      Is my understanding correct?
      Yes. Each invocation of a function gets its own private
      set of `auto' variables, distinct from those of other active
      functions. A function's own arguments behave the same way:
      they act like `auto' variables that are initialized by the
      function's caller, and each called function gets its own set.
      2) In this particular function, I noticed that as each recursion
      completes it's evaluation, 'k' is returned. I know where 'k' goes in
      the last evaluation, (back to "main") but where does it go, for lack of
      a better phrase, prior to that?
      If a function returns a value, the caller has an opportunity
      to make use of that value by storing it in a variable, adding it
      to some other value, printing it out, or whatever. The caller
      can also just ignore the returned value, in which case it "goes
      nowhere" and simply "disappears ." A caller is not obliged to use
      the value a function returns. (Did you know that printf() returns
      a value? Has non-use of that value ever bothered you?)
      [code snipped; see up-thread]
      --
      Eric Sosman
      esosman@acm-dot-org.invalid

      Comment

      • mdh

        #4
        Re: Recursive question.


        Kurt Weber wrote:
        >If I understand your explanation of your understanding correctly, then yes,
        your understanding is correct.
        >
        Of course, it could be that I misunderstand your understanding.
        >

        I understand!!! :-)

        Thank you.

        Comment

        • mdh

          #5
          Re: Recursive question.


          Eric Sosman wrote:
          >Yes. Each invocation of a function gets its own private
          set of `auto' variables, distinct from those of other active
          functions. A function's own arguments behave the same way:
          they act like `auto' variables that are in......

          Thank you Eric

          Comment

          • jacob navia

            #6
            Re: Recursive question.

            Kurt Weber wrote:
            [snip]

            The specs talked about a STRING, not a file.

            Comment

            Working...