Thread: Recursive Function

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    Question Recursive Function

    I am trying to work with the recursive function. I would like to implement a program using the recursive function power that when invoked, returns 3 to the power of 4. e.g., (3,4) = 3*3*3*3. I would like to assume that the exponent is an integer greater than or equal to 1. I would like to take the user input for the base.

    My thoughjt process is that the recursion step would use the relationship:

    base[exponent] = base*base [exponent]-1

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Looks good so far
    Now just write the code for it - pretty much as you've written it now really

    Dont forget the simple condition which stops the recursive calls.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If this is an excersice, it's a bad example of recursion. An iteration had been much better in this problem.

    Anyway, something like this should solve it:
    Code:
    int MyPower(int Base, int Exp)
    {
       if(Exp <= 0) return 1;
       else return Base * MyPower(Base, Exp - 1);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    cat < message_board > tutor

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    by the way

    hi all ,
    by the way ... how can we impement this funciton .... ?
    I mean how could they impement this fuction in C++.

    thanx
    C++
    The best

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, 11:27 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08: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, 01:27 PM