c++ - Why a return statement of my user defined function doesn't work at some case? -


a prime checking function of mine shows 9,15 etc prime aren't. code :

#include<iostream> #include<cstdio> using namespace std; int prime_function(int num, int i); int main(){     int num,flag=0;     while (cin>>num){         if(num!=1){             flag=prime_function(num,2);             if(flag==0)                 printf("%d isn't prime.\n",num);             else {                 printf("%d prime.\n",num);             }            }         else {             printf("%d prime.\n",num);         }     }     return 0; }  int prime_function(int num, int i) {     if(num%i==0){         printf("when num mod == 0, num=%d    i=%d\n",num,i);         return 0;//this statement doesn't work num=9,15...     }     else if((i*i)+1<=num){         printf("when num mod != 0, num=%d    i=%d\n",num,i);         prime_function(num,++i);     }     printf("going main function.\n");     return 1; } 

i made code pretty graphical errors can found easily. when input 9 program shows like:

when num mod != 0, num=9    i=2 when num mod == 0, num=9    i=3 going main function. going main function. 9 prime. 

it should print "going main function." once , come main function. doesn't , goes through entire function , comes main function. can me problem?

instead of

prime_function(num,++i); 

you want

return prime_function(num,++i); 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -