函数

11 阅读1分钟

写一个函数 用来求三个整数中的最大值 并返回

#include <stdio.h>

int getMax(int a, int b, int c) {
    int max = a > b ? a : b;
    return max > c ? max : c;
}

int main() {
    int result = getMax(15, 28, 10);
    printf("最大值是:%d", result);
 return 0;
}

运行结果 image.png

return 的作用

  1. 设置函数的返回值,它后边的表达式的值就是当前函数的返回值。
  2. 结束当前的执行,函数运行到这为止,它右边的代码都不会执行!
#include <stdio.h>
int f(){
	
    printf("a");
    printf("b");
    return 10 + 3 - 99 * 200;
    printf("c");
}

int main(){
    int rst = f();
    printf("%d", rst);
    return 0;
}