C语言中return的使用

没有返回值的参数:

void函数名不能使用带值的return,也可以没有return,调用的时候不能做返回值的赋值;

但是如果前面是int这类则需要返回一个对应的类型值,如果函数有返回值,则必须使用带值的return

#include <stdio.h>

//求最大值 

//第一种书写方法(return最后返回) 最好使用这种,单一出口的方式 
int max(int a,int b){
    int ret=0;
    if(a>b){
        ret=a; 
    }else{
        ret=b;
    }
    return ret; 
}

//第二种书写方法(多个return也可以) 没错,但是不符合单一出口的理念,不建议使用 
/*int max(int a,int b){
    if(a>b){
        return a; 
    }else{
        return b;
    }
}*/

int main()
{
    printf("%d\n",max(12,44));
    printf("%d\n",max(2,1));
    printf("%d\n",max(12,12));
    return 0;
 } 

 

posted @ 2023-03-29 20:07  周周周777  阅读(549)  评论(0)    收藏  举报