C语言中return的使用

发布时间 2023-03-29 20:13:41作者: 周周周777

没有返回值的参数:

void函数名可以没有return;如果函数有返回值,则必须使用带值的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;
 }