Just started programming in C and I'm using SAMS Teach Yourself C in 21 Days (I'm not holding them do that time frame though!)
Anyway, chapter 5 is all about functions which are fine and I understand. However, they briefly mention recursive functions. Now I understand the theory, the function calling itself until a condition is reached but I just can't follow in me head their example. I'm nearly there but . . . ah!
This is their code from an exercise question, can someone help me understand why the function doesn't return the value of 1 in every case?
Code:
#include <stdio.h>
int three_powered( int power );
main()
{
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\n", b, three_powered(b));
}
int three_powered( int power )
{
if ( power < 1 )
return ( 1 );
else
return( 3 * three_powered( power - 1 ));
}