Hello! I am currently trying to understand recursion and I am not quite sure why my code only prints "4"s. I am trying to understand what is going "under the hood." Any suggestions to clarify this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void pow (int x, int y)
{
if (y > 0)
{
pow (x, y - 1);
}
cout << x << endl;
x = x + 2;
}
int main ()
{
pow (2, 4);
}
#include<iostream>
int pow(int base, int p)
{
if (p == 0)
return 1;
else
return base*pow(base, p - 1);
}
using namespace std;
void main()
{
int base=0, p=0;
cout << "Enter the base and power " << endl;
cin >> base >> p;
cout<<"The result is "<<pow(base, p)<<endl;