Search code examples
cif-statementreturn

Is there a benefit/downside to return in an else statement?


Version 1:

int abc(...)
{
    if(a || b || c))
        return 1;
    else
        return 0;
}

Version 2:

int abc(...)
{
    if(a || b || c))
        return 1;

    return 0;
}

Is there any difference? Which code is the proper one?


Solution

  • "Is there any difference?"

    No.

    "Which code is the proper one?"

    None. They are equivalent. Note if the else isn´t explicitly needed for the context, omit it since it is redundant:

    Unnecessary 'else' after 'return'. (No-else-return)

    Note: The linked question is for Javascript, but it shares the same concern of yours.


    Furthermore, your code could be simplified:

    int abc (...)
    {
        return (a || b || c);
    }
    

    If the condition is true 1, else 0 is returned.