函数中有多个返回 [英] Multiple returns in functions

查看:90
本文介绍了函数中有多个返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中处理多个返回的优秀编程风格是什么?

函数返回不同的值?

- 要设置为返回的变量值,是在返回的函数的

结尾?

- 要有多个不同值的返回值?


哪个更喜欢,还有更好的方法吗?


/ nergal

What is a good programming style in C to handle multiple returns in a
function that returns different values?
- To have a variable that is set to the return value, which is in the
end of the function returned?
- To have multiple returns with different values?

Which to prefer, and is there a better way?

/nergal

推荐答案

nergal写道:
nergal wrote:

C中有什么好的编程风格来处理返回不同值的

函数中的多个返回值?

- 要有一个设置为返回值的变量,它返回函数的

结尾?

- 要有多个不同的返回值值?


哪个更喜欢,有更好的方法吗?
What is a good programming style in C to handle multiple returns in a
function that returns different values?
- To have a variable that is set to the return value, which is in the
end of the function returned?
- To have multiple returns with different values?

Which to prefer, and is there a better way?



可能是一种风格,但由于没有任何清理

mec在C中的hanism,我更喜欢C函数只返回一次。


它更容易调试(只有一个地方设置断点),节省了狩猎次数

多次返回,如果函数必须在返回之前释放任何锁或其他资源,生活会更轻松。


-

Ian Collins。

Probably a point of style, but due to the absence of any clean up
mechanism in C, I prefer a C function to have a single return.

It''s easier to debug (only one place to set a breakpoint), saves hunting
for multiple returns and makes life much easier if the function has to
release any locks or other resources before it returns.

--
Ian Collins.


nergal说:
nergal said:

什么是好的编程风格在C中处理返回不同值的

函数中的多个返回值?

- 要有一个设置为返回值的变量,该变量位于

返回的函数结束了吗?

- 要有多个不同值的返回值吗?


哪个更喜欢,有更好的方法?
What is a good programming style in C to handle multiple returns in a
function that returns different values?
- To have a variable that is set to the return value, which is in the
end of the function returned?
- To have multiple returns with different values?

Which to prefer, and is there a better way?



意见各不相同。我赞成单一的退货声明,而且我并不孤单。

。为什么?因为我认为它使代码更容易理解。但

很多人都希望尽快回来。为什么?因为他们认为它使代码更容易理解!


这不是正确性问题,而是风格问题。所以 - 你选择。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。

Opinions vary. I favour a single return statement, and I''m not alone in
that. Why? Because I think it makes the code easier to understand. But
many people prefer to return as soon as possible. Why? Because they
think it makes the code easier to understand!

This is not a matter of correctness, but of style. So - you choose.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


3月16日,12:52 pm,nergal ; < ner ... @ nergal.sewrote:
On Mar 16, 12:52 pm, "nergal" <ner...@nergal.sewrote:

在C中处理多个返回的好的编程风格

函数返回不同的值?

- 要有一个设置为返回值的变量,该函数在返回函数的

末尾?

- 要获得具有不同价值的多个回报吗?


哪个更喜欢,哪个更好?
What is a good programming style in C to handle multiple returns in a
function that returns different values?
- To have a variable that is set to the return value, which is in the
end of the function returned?
- To have multiple returns with different values?

Which to prefer, and is there a better way?



计划对你来说很容易。对于时间优化,您可以在函数中选择

多个返回语句。


-N

Program which seems easy to you. For time optimization, you can choose
multiple return statements in the function.

-N


这篇关于函数中有多个返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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