Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have made a program that i understand, this one:
// STYC page. 214, exc. 11.

#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:
// STYC page. 955, exc. 11.

#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 ) );
      // ( 3 ) * ( 3 ) * ( 3 ) * ( 3 ) ?
}


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.
Posted
Updated 29-May-21 22:39pm
v3

Quote:
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)?

This technique is recursion.
The code translate directly this recursive definition: Any power of 3 is 3 times the previous power of 3, and power 0 of is equal to 1.
Recursion (computer science) - Wikipedia[^]
C program to calculate the power using recursion[^]
 
Share this answer
 
v2
That is what is known as a recursive function. The original value power is passed in to the function, which then calls itself with the value power - 1 and returns the answer multiplied by 3. The recursed call (with power - 1), does the same again until the value passed in is less than 1 (i.e zero). If you step through the code with the debugger you can see how each value is calculated being repeatedly multiplied by 3.

if The original value of power is 5 we get:
return 3 * (3 * (3 * (3 * (3 * (1))))) = 3 * 3 * 3 * 3 * 3 = 243 or 3 to the power 5.
 
Share this answer
 
Comments
Mieczyslaw1683 30-May-21 10:15am    
Okay. So I have checked recursion on Wikipedia. Then I have checked the Debugger in CodeBlocks (I am using CodeBlocks). I understand how "Watches" works where I can see the variable numbers. I also understand the "Run to cursor" and "Next line" and "Step into" buttons now.

However is there maybe something I missed in the debugger because I dont see any multiplications?
Richard MacCutchan 30-May-21 10:34am    
The multiplications happen on the return statements:
Multiply 3 by the return value from the recursed call ... and return it back to the next level. So the very last return will effectively be what I showed above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900