“返回"是什么意思声明在Objective-C中是什么意思? [英] What does "return" statement mean in Objective-C?

查看:75
本文介绍了“返回"是什么意思声明在Objective-C中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Objective-C>中return语句是什么意思?

What is the meaning of return statement in Objective-C>?

例如一个简单的计算器方法来添加数字:

For example, a simple calculator method to add numbers:

-(double) add: (double) value
{
    accumulator += value;
    return accumulator;
}

这是否意味着结果将返回(或存储?)回累加器?如果在那种情况下没有 return 语句怎么办?

Does it mean that the result will be returned (or stored?) back to accumulator? What if there would be no return statement in that case?

另外,如何解释语句return 0;"我们写在每个程序的末尾.

Also, how to explain the statement "return 0;" that we write in the end of each program.

提前致谢.

推荐答案

return 语句中给出的值是函数或方法返回的值.在您的示例中,accumulator 中的值将是调用方法 -add: 的结果,如下所示:

The value given in the return statement is the value returned by the function or method. In your example, the value in accumulator will be the result of calling the method -add:, like this:

double bar = [foo add:3.1];

bar 将获得 accumulator 中的值.

另外,如何解释语句return 0;"我们写在每个程序的末尾.

Also, how to explain the statement "return 0;" that we write in the end of each program.

在 Unix 中(记住,MacOS X 和 iOS 都是 Unix 的风格),程序在退出时返回一个值.值 0 表示正常终止;其他值表示某种错误或异常终止.

In Unix (and remember, both MacOS X and iOS are flavors of Unix), programs return a value when they exit. The value 0 indicates normal termination; other values indicate some sort of error or abnormal termination.

这篇关于“返回"是什么意思声明在Objective-C中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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