How does return work here?

前端 未结 5 587
野性不改
野性不改 2021-01-27 06:25

new question: but why the expression from n == number, from firstFactorPtr == factor1 ?

Can somebody explain me, how this works:

return ( factor ==

5条回答
  •  失恋的感觉
    2021-01-27 06:39

    Summarizing your Factor function:

    bool Factor( /* ... */ )
    {
        int factor;
        /* ... */
        factor = /* some value */;
        /* ... */
        return ( factor == 1 );
    }
    

    The simple explanation is that the expression ( factor == 1 ) is true if factor is equal to 1, false if it's unequal. This value, either true or false, is returned from the function. A call to the function is a Boolean expression, just like a call to a function that returns an int is an int expression. Which means that you can write something like this:

    /* some computations */
    if (factor == 1) {
        /* do something */
    }
    

    you can write this:

    if (Factor( /* arguments */ )) {
        /* do something */
    }
    

    with the computations performed inside the Factor function.

    Boolean values like "true" and "false" are just values, and they can be manipulated like any other values like 42 or 1.23.

    But it's a bit more complicated than that.

    == is the equality operator. There's nothing particularly special about it; like any other operator (+ for addition, / for division, etc.) it takes one or more operands of some type(s) and yields a result of some type.

    + yields the sum of its operands; the type of the result depends on the type of the operands.

    == yields a result of type int. That result is 1 if the operands are equal to each other, 0 if they're not. In this case, the operands factor and 1 are of the same numeric type, int, so the comparison is valid; there are fairly complicated rules for == on other scalar types, but we needn't worry about them here.

    For historical reasons, the result is not of type bool, though that would make more sense. C didn't even have a Boolean type until the 1999 standard; rather, it's of type int. Any scalar (integer, floating-point, or pointer) expression can be used in a Boolean context (if, while, etc.), treated as false if it's equal to 0 and true otherwise.

    So if the value of factor happens to be 1 when the return statement is executed, it will return the value 1.

    Your Factor function is defined to return a result of type bool, not int. So the value of the expression is implicitly converted from int (the type yielded by ==) to bool (the type of the function result). The result of that conversion is simply 1, but of type bool rather than int. That value can also be spelled as true since you've #included , which defines true as a macro.

    (Some other languages have had a Boolean type from the beginning. In such a language, the expression ( factor == 1 ) would yield a Boolean value of "true", and that value would be returned by the function, with no conversion necessary. The behavior would be conceptually simpler than what happens in C, but the result would be essentially the same. When was added to the language, along with the type _Bool and the macros false and true, it was done in a way that makes it convenient to write code that uses the new built-in Boolean type without breaking existing code that uses int or other scalar types for Boolean values and operations.)

提交回复
热议问题