recursive function

F

fr3shman

------------------------------------------------------
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
 
D

dbtid

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.
 
M

Martin Ambuhl

------------------------------------------------------
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
 
T

Taran

------------------------------------------------------
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

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
 
R

Rufus V. Smith

------------------------------------------------------
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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top