How does return work here?

前端 未结 5 586
野性不改
野性不改 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 <stdbool.h>, 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 <stdbool.h> 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.)

    0 讨论(0)
  • 2021-01-27 06:48

    return (factor == 1) means 'return true if factor == 1, return false if factor != 1'. The function called Factor() returns true if the value of the 'factor' variable went all the way down to 1 (meaning that it couldn't find any factors for the 'number' variable). The function returns false if 'factor' did not make it all the way down to 1, meaning that it was able to find at least one factor for 'number'.

    0 讨论(0)
  • 2021-01-27 06:52

    In a nutshell:

     return foo == bar;
    

    is equivalent to

     return foo == bar ? 1 : 0;
    

    since relational operators like == yield either 0 or 1. Does that make it clear?

    0 讨论(0)
  • 2021-01-27 06:53

    It returns the result of the Boolean expression "factor equals 1", i.e., true or false.

    This routine also has a side effect: it stores the found factorials in the lists pointed to by the two int * in the header.

    In essence, it returns true if the factor is a prime -- the loop counted down all the way to '1' --, false otherwise.

    0 讨论(0)
  • 2021-01-27 06:53

    factor counts all the way down from sqrt to 2. If there is a factor which is divisible by the number, it breaks out of the for loop. Thus factor contains the one of the divisors of number. So the divisor is held by *firstFactor and the second divisor is held by *secondFactorPtr. Note that these are the same pointers which your main() function has passed to Factor. So in the main() function you will find that factor1 and factor2 are populated with 2 factors.

    If it does not find any factor greater than 1, then it contains 1 and breaks out of the for loop. If it does not find a factor, then the only factos are 1 and the number itself.

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