Need help with simple recursive function

Hey all, I am looking for help in understanding how recursive functions work. What I want is a function to be given two integer paramaters x and n, where the recursive function is essentially supposed to calculate x^n noting that x^(n-1)*x=x^n. The function should return the proper answer.

Here is the code regarding the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  int power(int x, int n)
{
     int const y = x; //I did this so that we never lose what the initial value of x is.
     if (n ==1)
     {
          return x;
     }
     else if (n==0)
     {
          return 1;
     }
     else
     {
          x=x*y; n--; return power(x, n);
     }
}
1
2
3
4
int power(int x, unsigned int n)
{
   return ( ( n == 0 ) ? 1 : x *power( x, --n ) );
}
Not sure if you are joking, but I thought this was a "beginner" forum. I haven't even seen the use of "?" or ":".
Obviously it worked, but what good is a solution if one has not yet learned 3/4 of the concepts in the solution?
it means

expression ? dothisifitstrue: dothisifitsfalse

basically it could have been written in an easier way

1
2
3
4
5
6
7
int power(int x, int n){
     if(n==0){
         return 1;
     }
     else 
          return x* power(x, n-1);
}


ninja edit:

basically if you use this function on the numbers 2, 3 which is 2 to the 3 power

the initial call to the function sees n is greater than 0 so it attempts to return the power function 2, 2 times the original 2

this dives into recursion and you end up having the next recursive call do the same except now it calls power 2,1

and this continues until you reach 2,0 where the recursion ends and it chains back up to the result.


its hard to explain without drawing it, youtube is a good resource since they can get visual with the topic
Last edited on
As a matter of fact the function has a defect. It would be better to define it the following way


1
2
3
4
constexpr int power(int x, unsigned int n)
{
   return ( ( n == 0 ) ? 1 : x *power( x, n - 1 ) );
}


For example

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
 
constexpr int power(int x, unsigned int n)
{
   return ( ( n == 0 ) ? 1 : x *power( x, n - 1 ) );
}
 
int main()
{
    const int a[power( 2, 5 )] = { };
    
    std::cout << "The size of array 'a' is " << sizeof( a ) / sizeof( *a ) << std::endl;
}
Perfect, thanks a lot! Gonna work on a few more simple recursive functions.
I'm working on another recursive function. I want to use ONLY recursive calls, no loops. The program should print n asterisks on a single line, then print a new line, and then print one less asterisk, until there are no more. For example, calling printStars(5) would do:
*****
****
***
**
*


1
2
3
4
5
6
7
8
9
10
11
12
13
14
void printStars(int n)
{
	
	
		if(n > 0)
		{
			cout << '*';
			return printStars(n-1);
		}
		cout << endl;
		
	
return;
}


Once it recursively calls the function n times, n is then zero and the function is done. How can I set it up so that n does not go to zero so I can start a new line? I can easily do this problem with a loop, but i want to with only recursive functions. Thanks
Topic archived. No new replies allowed.