Recursive function

This is a discussion on Recursive function within the C++ Programming forums, part of the General Programming Boards category; I am still learning this. Does this look right? Thanks!! Code: Void DoIt(int N) { if ( N > 0) ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    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);
       }
    }

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    *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.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    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?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    it would work, but forever(or a crash)
    Last edited by prog-bman; 06-16-2004 at 12:19 PM.
    Woop?

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    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

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    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.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By Fork in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 12:27 PM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21