函数综合示例
把「读想法 → 拆函数」练一遍:主流程保持短。
1. 完整例子
| 1 | #include <stdio.h> |
| 2 | |
| 3 | int max2(int a, int b) { |
| 4 | return (a > b) ? a : b; |
| 5 | } |
| 6 | |
| 7 | void print_line(void) { |
| 8 | printf("----------\n"); |
| 9 | } |
| 10 | |
| 11 | int main(void) { |
| 12 | print_line(); |
| 13 | printf("max=%d\n", max2(3, 9)); |
| 14 | print_line(); |
| 15 | return 0; |
| 16 | } |
2. 设计小任务
写一个「三数求最大」:先写 max2,再用 max2 组合出 max3。体会函数复用。
| 1 | int max3(int a, int b, int c) { |
| 2 | return max2(max2(a, b), c); |
| 3 | } |