# Recursive function

• 06-16-2004
campermama
Recursive function
I am still learning this. Does this look right?
Thanks!!
Code:

``` Void DoIt(int N) { if ( N > 0) { cout << N << " "; DoIt(N + 1);   } }```
• 06-16-2004
jverkoey
*looks around*

what's it supposed to do? looks to me that if you put in any value >=1, it'll loop forever till the stack overflows, anything <1 won't even be displayed.
• 06-16-2004
campermama
I am just supposed to write a recursive function, just not sure if this is "good" or not. If it was in a program would it work?
• 06-16-2004
prog-bman
it would work, but forever(or a crash) :D
• 06-16-2004
jverkoey
The function works fine, it recursively calls itself. However, you either a) put the limit in the wrong direction or b) forgot to put a limit in there.

If you don't have a limit in the function it will keep calling itself until your program crashes. I'd suggest putting something like if(N>=50) return; in there or something so that it stops at 50
• 06-16-2004
jlou
You could also change this:
Code:

`DoIt(N + 1);`
to this:
Code:

`DoIt(N - 1);`
Then, if you pass in any number it will print a countdown to 1.
• 06-17-2004
major_small
why not create a more useful recursive function... try writing something similar to the pow() function...

Code:

```long double pow(int base,int exp) {     if(exp==0)           return 1;     else if (exp>1)           //recursiveness goes here     else           //you figure out what goes here }```
fill in the blanks...