退货如何在这里工作? [英] How does return work here?

查看:24
本文介绍了退货如何在这里工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下,这是如何工作的:

Can somebody explain me, how this works:

返回(因子 == 1);

return ( factor == 1 );

这将返回到main";用因子 == 1",为什么isPrime"?返回真?
如果我们*firstFactorPtr",为什么它会返回假?和*secondFactorPtr"?它正在搜索素数和非素数.
没有整数因子";在main"中,如何返回到main"?有什么表情吗?

This returns to "main" with "factor == 1 " , why does "isPrime" return true?
And why does it return false if we "*firstFactorPtr" and "*secondFactorPtr" ? It is searching for prime numbers and for non prime numbers.
There is no "int factor" in "main", how can it return to "main" with any expression?

 #include <stdio.h>
 #include <stdbool.h>
 #include <math.h>


    bool Factor( int number, int *firstFactorPtr, int *secondFactorPtr );

    int main(int argc, const char * argv[])
   {
    int n;

for ( n = 5; n <= 20; n++ ) {
    bool isPrime;
    int factor1, factor2;
    
    isPrime = Factor( n, &factor1, &factor2 );
    if ( isPrime )
        printf( "the number %d is prime
", n );
    else
        printf( "the number %d has %d and %d as factors
", n, factor1, factor2 );
    }

  return 0;
 }


 bool Factor( int number, int *firstFactorPtr, int *secondFactorPtr )
 {
if ( number < 1 )
    return false;   

int factor; 
for ( factor = sqrt(number); factor > 1; factor-- ) {
    if ( (number % factor) == 0 ) {
        break;
    }
}

*firstFactorPtr = factor;
*secondFactorPtr = number / factor;
return ( factor == 1 );
 }

推荐答案

总结你的Factor函数:

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

简单的解释是表达式 ( factor == 1 ) 如果 factor 等于 1 则为 true,如果不相等则为 false.该值(真或假)从函数返回.对函数的调用是一个布尔表达式,就像对返回 int 的函数的调用是一个 int 表达式一样.这意味着你可以这样写:

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 */
}

你可以这样写:

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

Factor 函数中执行计算.

with the computations performed inside the Factor function.

true"和false"等布尔值只是值,它们可以像 421.23 等任何其他值一样进行操作.

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

但比这要复杂一些.

== 是相等运算符.它没有什么特别之处.像任何其他运算符(+ 用于加法,/ 用于除法等)一样,它采用某种类型的一个或多个操作数并产生某种类型的结果.

== 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.

== 产生 int 类型的结果.如果操作数彼此相等,则结果为 1,否则为 0.在这种情况下,操作数factor1是相同的数值类型,int,所以比较有效;== 在其他标量类型上有相当复杂的规则,但我们不必在这里担心它们.

== 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.

由于历史原因,结果是 not 类型为 bool,尽管这样更有意义.直到 1999 年的标准,C 甚至都没有布尔类型.相反,它的类型是 int.任何标量(整数、浮点或指针)表达式都可以在布尔上下文(ifwhile 等)中使用,如果等于0 否则为真.

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.

所以如果在执行return语句时factor的值恰好是1,就会返回1的值.

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

您的 Factor 函数被定义为返回 bool 类型的结果,而不是 int.所以表达式的值从int(==产生的类型)隐式转换为bool(函数结果的类型).该转换的结果只是 1,但类型为 bool 而不是 int.该值也可以拼写为 true,因为您已经 #included <stdbool.h>,它定义了 true 作为一个宏.

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.

(其他一些语言从一开始就具有布尔类型.在这种语言中,表达式 ( factor == 1 ) 将产生一个布尔值true",并且该值将由函数返回,无需转换.行为在概念上比 C 中发生的更简单,但结果基本相同.当 <stdbool.h> 被添加到语言,连同类型 _Bool 和宏 falsetrue,它的完成方式便于编写使用新的内置布尔类型没有破坏使用 int 或其他标量类型的布尔值和操作的现有代码.)

(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.)

这篇关于退货如何在这里工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆