Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c program Warning the control reaches the end of non-void function

so this is a simple c programming using recursion which returns the power of the input value, but for some reason it showing error

#include <iostream>
//int sum = 1;

int powerOfNumber(int n, int p) {
    if (n != 0) {
        p--; 
        return powerOfNumber(n , p) * n;
    }
    if (n == 0) {
        return 1;
    }
}


int main()
{
    std::cout <<powerOfNumber(5, 2);
   

    return 0;
    
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <iostream>

using namespace std; 

int powerOfNumber(int n, int p) {
    if(p==0)
        return 1;
    else 
        return (n*powerOfNumber(n,p-1));     
}


int main()
{
    std::cout <<powerOfNumber(5, 2);

    return 0;
}

Your powerOfNumber function never terminates because n is always 5.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading