MSDN 中的返回语句
今天在看MSDN的时候,遇到了下面的代码:
void draw( int I, long L );
long sq( int s );
int main()
{
long y;
int x;
y = sq( x );
draw( x, y );
return();
}
long sq( int s )
{
return( s * s );
}
void draw( int I, long L )
{
/* Statements defining the draw function here */
return;
}
当然,这不起作用,所以我改变了
返回();
在主要功能中
返回0;
它的工作需要谨慎。 我对这段代码有两个问题:
1.为什么微软使用return();这只是一个错误?还是其他原因?
2.返回什么;在draw函数中是什么意思?我认为没有必要,为什么它存在于函数中?
Today,when I was reading the MSDN,I encountered the following codes:
void draw( int I, long L );
long sq( int s );
int main()
{
long y;
int x;
y = sq( x );
draw( x, y );
return();
}
long sq( int s )
{
return( s * s );
}
void draw( int I, long L )
{
/* Statements defining the draw function here */
return;
}
Of course,it didn't work,so I change the
return();
in main function to
return 0;
It works with a caution .
I have two problems about this code:
1.Why does Microsoft use return ();Is this just a mistake?Or other reasons?
2.what does return; in the draw function mean?I think it is not necessary,why does it exsit in the function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我同意其他海报,即使在这种情况下绘制函数中的返回不是必需的,因为函数末尾有一个隐式返回语句,但它是允许的,并且确实可以用于提前退出函数这样就可以避免函数中的更多代码,例如
I agree with the other posters, also even though the return in the draw function isn't necessary in this case, as there's an implicit return statement at the end of the function, it is allowable and indeed may be used to exit a function early so that further code in a function is avoided e.g.
显然我不能说出代码作者的意思,但对于第一个问题,我认为这是一个错误,作者的意思是写
return(0);
。对于第二个问题,你认为是对的。
return
是不需要的,而且同样无法回答作者为什么把它放在那里。Obviously I can't say what the author of the code meant, but for the first question I would think it's a mistake and that the author meant to write
return(0);
.For the second question you think right. The
return
is not needed, and again it's impossible to answer why the author put it there.