如何退出void函数 [英] How to exit a void function

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

问题描述

我有这个功能,当我使用普通的编译器时它很糟糕,但是当我使用keil时,它给了我一个错误,我们可以在void函数中使用return。

我怎么能退出if条件之后的这个功能。

提前谢谢



我尝试了什么:



 #include< stdio.h> 
int onevalue(void){


返回1;
}



void main(void)
{
printf(Hello World \ n);
if(onevalue()== 1)
{
printf(Error)
//此处停止此函数(main函数)的表达是什么
}
for(int i = 0; i< 3; i ++)
printf(here:%d \ n,i);


}

解决方案

正确的陈述是

< pre lang =C> return ;





例如:

 如果(onevalue()==  1 
{
printf( 错误);
// 此处停止此功能的主要内容是什么(主要功能)
return ; // < - 它的回归!
}


函数中可以有多个返回值。由于这是main(),你可能希望改为考虑一个退出函数。



然而,没有理由你不能使用:

 if(onevalue()== 1){
printf(Error)
return;
}





或者,把整个事情包起来:

 void main(){
if(onevalue()== 1){
printf(Error)
}
else {
for(int i = 0; i< 3; i ++)
printf(here:%d \ n,i);
}
返回;
}


i have this fucntion , when i use a normal compiler it wors , but when i use keil , it gives me an error that we can use return in void function .
how can i exit from this function after the if condition.
thank you in advance

What I have tried:

#include <stdio.h>
int onevalue(void){
    
    
    return 1;
}



void main(void)
{
    printf("Hello World \n");
    if (onevalue()==1)
    {
        printf ("Error")
      // what is the experssion here to stop this function (main function)  
    }
    for (int i = 0; i<3; i++)
    printf ("here :%d \n" ,  i);


}

解决方案

the correct statement is

return;



For instance:

if (onevalue()==1)
    {
        printf ("Error");
        // what is the experssion here to stop this function (main function)  
        return; //<-- it's return!
    }


You can have more than one return in a function. Since this is main(), you may wish, instead, to consider an exit function, instead.

There should be no reason, however, that you cannot use:

if (onevalue()==1)  {
        printf ("Error")
        return;
}



or, wrap the whole thing up as:

void main() {
 if (onevalue()==1)  {
   printf ("Error")
 }
 else {
   for (int i = 0; i<3; i++)
   printf ("here :%d \n" ,  i);
 }
 return;
}


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

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