I have made a program that i understand, this one:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
float a = 0;
float b = 0;
printf("You have 3. To the power of what number do you want it now? Type in a number: ");
scanf("%f", &a);
b = pow(3, a);
printf("3 to the power of %f is: %f", a, b);
return 0;
}
Then there is a solution in the book im learning from, that looks like this:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int three_powered( int power );
int main( void )
{
int a = 4;
int b = 9;
printf("\n3 to the power of %d is %d", a, three_powered(a) );
printf("\n3 to the power of %d is %d", b, three_powered(b) );
return 0;
}
int three_powered( int power )
{
if (power < 1)
return ( 1 );
else
return ( 3 * three_powered( power - 1 ) );
}
I do not understand how this three_powered( int power) function is working iteration by iteration (or what the stages are called in this type of function)?
What I have tried:
I have tried to write a comment with the calucating stages taking place however, I still dont understand how to read the functions code and understand it.