什么是结束一个void函数的正确方法是什么? [英] What is the correct way to end a void function?
问题描述
这是结束void函数比较正确的做法:
void函数(){
//等等一些code
}
或
void函数(){
//等等一些code
返回;
}
理由第二种方式:
- 这前presses开发商的意图更清晰。
- 它有助于在pre-编译时间检测功能结束:
假设你有这样的scenario-你有一大堆的功能,你必须在这些功能的最终注入一些code。但是,出于某些原因,你不希望/或无法修改如此巨大的功能数量。你可以做些什么? 返回
&安培; 宏
进场,例如:
#包括LT&;&stdio.h中GT;#定义MAX_LINES 1000
#定义XCAT(A,B)一## b
#定义CAT(A,B)XCAT(A,B)
#定义返回返回[__ LINE__] = 1; \\
如果(返回[__ __ LINE])\\
{printf的(对%D线功能结束\\ n,__ __ LINE);} \\
INT CAT(TMP,__ LINE__); \\
如果((CAT(TMP,__ __ LINE)=返回[__ LINE__],返回[__ LINE__] = 0,CAT(TMP,__ __ LINE)))\\
返回静态int返回[MAX_LINES]
虚空功能1(无效){
返回;
}无效函数2(无效){
返回;
}诠释的main()
{
功能1();
函数2(); 返回0;
}
无论是比较正确的,所以你挑。空的回归;提供语句允许从除结尾以外的某处一个void函数的返回。没有其他原因,我相信了。
Which is more correct way to end void function:
void function() {
// blah some code
}
OR
void function() {
// blah some code
return;
}
Rationale for second way:
- It expresses developer intentions more clearly.
- It helps detecting function end at pre-compile time:
Suppose you have such scenario- you have bunch of functions and you must inject some code at the end of those functions. But for some reasons you don't want / or can't modify such huge amount of functions. What can you do about that ? Return
& macro
comes into play, for example:
#include<stdio.h>
#define MAX_LINES 1000
#define XCAT(a,b) a##b
#define CAT(a,b) XCAT(a,b)
#define return returns[__LINE__] = 1;\
if (returns[__LINE__])\
{printf("End of function on %d line.\n",__LINE__);}\
int CAT(tmp,__LINE__); \
if ((CAT(tmp,__LINE__)=returns[__LINE__], returns[__LINE__] = 0, CAT(tmp,__LINE__)))\
return
static int returns[MAX_LINES];
void function1(void) {
return;
}
void function2(void) {
return;
}
int main()
{
function1();
function2();
return 0;
}
Neither is more correct, so take your pick. The empty 'return;' statement is provided to allow a return in a void function from somewhere other than the end. No other reason I believe.
这篇关于什么是结束一个void函数的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!