Confused about the function return value

前端 未结 6 1871
予麋鹿
予麋鹿 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:51

    The code you have has a couple of errors:

    • you have an int being assigned the value 1.0 (which will be implicitly cast/converted), not an error as such but inelegant.
    • you have a return statement inside a conditionality, so you will only ever get a return when that if is true

    If you fix issue with the return by removing the else, then all will be fine:)

    As to why it works with 4 as an input, that is down to random chance/ some property of your environment as the code you have posted should be unable to function, as there will always be an instance, when calculating factorials for a positive int, where x = 1 and no return will be generated.

    As an aside, here is a more concise/terse function: for so straightforward a function you might consider the ternary operator and use a function like:

    int factorial(int x){ return (x>1) ? (x * factorial(x-1)) : 1;}
    

    this is the function I use for my factorials and have had on library for the last 30 or so years (since my C days) :)

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2021-01-15 12:01

    As stated in the comment this is undefined behaviour. With g++ I get the following warning.

    warning: control reaches end of non-void function [-Wreturn-type]
    

    On Visual C++, the warning is promoted to an error by default

    error C4716: 'Fun' : must return a value
    

    When I disabled the warning and ran the resulting executable, Fun(4) gave me 1861810763.

    So why might it work under g++? During compilation conditional statements are turned into tests and jumps (or gotos). The function has to return something, and the simplest possible code for the compiler to produce is along the following lines.

    int Fun(int x)
    {
        int sum=1.0;
        if(!(x>1))
            goto RETURN;
        sum=x*Fun(x-1);
    RETURN:
        return sum;
    }
    

    This is consistent with your disassembly.

    Of course you can't rely on undefined behaviour, as illustrated by the behaviour in Visual C++. Many shops have a policy to treat warnings as errors for this reason (also as suggested in a comment).

    0 讨论(0)
  • 2021-01-15 12:03

    Just remove else from your code:

    int Fun(int x)
    {
        int sum=1;
        if(x>1)
            sum=x*Fun(x-1);
    
        return sum;
    }
    
    0 讨论(0)
  • 2021-01-15 12:16

    Usually, EAX register is used to store return value, ad it is also used to do other stuff as well.

    So whatever has been loaded to that register just before the function returns will be the return value, even if you don't intend to do so.

    You can use the -S option to generate assembly code and see what happened to EAX right before the "ret" instruction.

    0 讨论(0)
  • 2021-01-15 12:18

    From the C standards:

    Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

    Your situation is the same as this one:

    int fun1(int x)
    {
        int sum = 1;
        if(x > 1)
            sum++;
        else
            return sum;
    
    }
    int main()
    {
        int b = fun1(3);
        printf("%d\n", b);
        return 0;
    }
    

    It prints 2 on my machine. This is calling convention and architecture dependent. The return value is the result of last expression evaluation, stored in the eax register.

    0 讨论(0)
提交回复
热议问题