recursive function

Discussion in 'C Programming' started by fr3shman@gmail.com, Jan 16, 2005.

  1. Guest

    ------------------------------------------------------
    main()
    {
    void kk();
    kk(0);
    getch();
    }

    void kk(j)
    int j;
    {
    if (j<10)
    {
    j++;
    printf("the number is:%d\n",j);
    kk(j);
    printf("the number is k%d\n",j);
    }
    }
    -------------------------------------------------------
    I thought the program would printf:
    the number is:1
    the number is:2
    the number is:3
    the number is:4
    the number is:5
    the number is:6
    the number is:7
    the number is:8
    the number is:9
    the number is:10
    -------------------------------------------------------
    but,the result is:
    Invalid keyboard code specified
    the number is:1
    the number is:2
    the number is:3
    the number is:4
    the number is:5
    the number is:6
    the number is:7
    the number is:8
    the number is:9
    the number is:10
    the number is k10
    the number is k9
    the number is k8
    the number is k7
    the number is k6
    the number is k5
    the number is k4
    the number is k3
    the number is k2
    the number is k1
    -----------------------------------------------------
    why? thanks...
    btw,sorry for my poor english.:)
    , Jan 16, 2005
    #1
    1. Advertising

  2. dbtid Guest

    Well, it does do that, but that second printf is going to execute after
    the recursive calls are completed.

    Try tracing out the control flow on a piece of paper and I think you'll
    learn a lot.
    dbtid, Jan 16, 2005
    #2
    1. Advertising

  3. wrote:
    > ------------------------------------------------------
    > main()
    > {
    > void kk();
    > kk(0);
    > getch();
    > }
    >
    > void kk(j)
    > int j;
    > {
    > if (j<10)
    > {
    > j++;
    > printf("the number is:%d\n",j);
    > kk(j);
    > printf("the number is k%d\n",j);
    > }
    > }



    Compare your code to the following:

    #include <stdio.h>

    void kk(int);

    int main(void)
    {
    kk(0);
    return 0;
    }

    void kk(int j)
    {
    if (j < 10) {
    j++;
    printf("the number is:%d\n", j);
    kk(j);
    }
    }

    the number is:1
    the number is:2
    the number is:3
    the number is:4
    the number is:5
    the number is:6
    the number is:7
    the number is:8
    the number is:9
    the number is:10
    Martin Ambuhl, Jan 16, 2005
    #3
  4. Taran Guest

    wrote:
    > ------------------------------------------------------
    > main()
    > {
    > void kk();
    > kk(0);
    > getch();
    > }
    >
    > void kk(j)
    > int j;
    > {
    > if (j<10)
    > {
    > j++;
    > printf("the number is:%d\n",j);
    > kk(j);
    > printf("the number is k%d\n",j);
    > }
    > }
    > -------------------------------------------------------
    > I thought the program would printf:
    > the number is:1
    > the number is:2
    > the number is:3
    > the number is:4
    > the number is:5
    > the number is:6
    > the number is:7
    > the number is:8
    > the number is:9
    > the number is:10
    > -------------------------------------------------------
    > but,the result is:
    > Invalid keyboard code specified
    > the number is:1
    > the number is:2
    > the number is:3
    > the number is:4
    > the number is:5
    > the number is:6
    > the number is:7
    > the number is:8
    > the number is:9
    > the number is:10
    > the number is k10
    > the number is k9
    > the number is k8
    > the number is k7
    > the number is k6
    > the number is k5
    > the number is k4
    > the number is k3
    > the number is k2
    > the number is k1
    > -----------------------------------------------------
    > why? thanks...
    > btw,sorry for my poor english.:)


    The output is correct and outputs what your code commands it to do.

    For the first call to kk at kk(0), j is incremented and the first
    printf prints j value 1. There there's a call to kk(1), j is
    incremented and pritnf prints 2 and so on. After some calls to kk where
    kk(10) happens then if block is 'jumped over' and the recursive
    funtions starts to roll back that's when the second printf prints
    ".....k9.." and so on.
    Try doing doing a dry run, or control flow for some iteration with
    /**/ if (j<3)
    /**/ {
    /**/ j++;

    you'll understand it youself.

    HTH.
    Regards,
    Taran
    TT
    Taran, Jan 17, 2005
    #4
  5. <> wrote in message
    news:...
    > ------------------------------------------------------
    > main()
    > {
    > void kk();
    > kk(0);
    > getch();
    > }
    >
    > void kk(j)
    > int j;
    > {
    > if (j<10)
    > {
    > j++;
    > printf("the number is:%d\n",j);
    > kk(j);
    > printf("the number is k%d\n",j);
    > }
    > }
    > -------------------------------------------------------
    > I thought the program would printf:
    > the number is:1
    > the number is:2
    > the number is:3
    > the number is:4
    > the number is:5
    > the number is:6
    > the number is:7
    > the number is:8
    > the number is:9
    > the number is:10
    > -------------------------------------------------------
    > but,the result is:
    > Invalid keyboard code specified
    > the number is:1
    > the number is:2
    > the number is:3
    > the number is:4
    > the number is:5
    > the number is:6
    > the number is:7
    > the number is:8
    > the number is:9
    > the number is:10
    > the number is k10
    > the number is k9
    > the number is k8
    > the number is k7
    > the number is k6
    > the number is k5
    > the number is k4
    > the number is k3
    > the number is k2
    > the number is k1
    > -----------------------------------------------------
    > why? thanks...
    > btw,sorry for my poor english.:)
    >


    That is what you programmed! Why did you expect the results you listed?

    Can't tell you why the keyboard code error came up, but the reason it's at
    the beginning
    is probably because you didn't flush stdout.

    But take out your second printf line if you don't want those to print as
    your stack "unwinds"
    from those recursive calls.


    perhaps you should have written kk() as:

    void kk(j)
    int j;
    {
    if (j<10)
    {
    j++;
    printf("before recursive call. j=:%d\n",j);
    kk(j);
    printf("call returned, j=%d\n",j);
    }
    }

    and perhaps you should do a fflush(stdout) before the getch();

    Rufus
    Rufus V. Smith, Jan 20, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tyron

    Recursive function

    Tyron, Apr 16, 2004, in forum: VHDL
    Replies:
    1
    Views:
    627
    valentin tihomirov
    Apr 16, 2004
  2. n00m
    Replies:
    12
    Views:
    1,112
  3. vamsi
    Replies:
    21
    Views:
    2,072
    Keith Thompson
    Mar 9, 2009
  4. Mark Piffer
    Replies:
    9
    Views:
    907
    luserXtrog
    May 15, 2009
  5. Alok
    Replies:
    3
    Views:
    249
Loading...

Share This Page