return statement

来自cppreference.com
TranslationBot留言 | 贡献2012年10月25日 (四) 20:00的版本 (Translated from the English version using Google Translate)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

<metanoindex/>


 
 
C++ 语言
 
。终止当前函数的调用函数返回指定的值.
原文:
Terminates current function and returns specified value to the caller function.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

。语法。

return expression (1)
return (2)

。解释。

。的第一个版本的计算结果expression,终止当前函数,并返回结果的expression呼叫者函数。 expression的结果类型的,必须转换为函数的返回类型.
原文:
The first version evaluates the expression, terminates the current function and returns the result of the expression to the caller function. The resulting type of the expression must be convertible to function return type.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
。第二个版本终止当前的功能。如果函数的返回类型是唯一有效的void.
原文:
The second version terminates the current function. Only valid if the function return type is void.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

。关键字。

return

。为例。

#include <iostream>

void fa(int i) 
{
    if (i == 2) return;
    std::cout << i << '\n';
}

int fb(int i) 
{
    if (i > 4) return 4;
    std::cout << i << '\n';
    return 2;
}

int main() 
{
    fa(2);
    fa(1);
    int i = fb(5);
    i = fb(i);
    std::cout << i << '\n';
}

输出:

1
4
2