Confused about the function return value

前端 未结 6 1869
予麋鹿
予麋鹿 2021-01-15 11:12
#include
using namespace std;
int Fun(int x)
{
    int sum=1;
    if(x>1)
        sum=x*Fun(x-1);
    else
        return sum;
}
int main()
{
             


        
6条回答
  •  时光说笑
    2021-01-15 11:54

    When your program pass in the if condition, no return statement finish the function. The number you got is the result of an undefined behavior.

    int Fun(int x)
    {
        int sum=1.0;
        if(x>1)
            sum=x*Fun(x-1);
        else
            return sum;
    
        return x; // return something here
    }
    

提交回复
热议问题