定义与调用
声明说明长相;定义给出实现;调用真正执行。
1. 完整例子
| 1 | #include <stdio.h> |
| 2 | |
| 3 | int add(int a, int b); // 声明 |
| 4 | |
| 5 | int main(void) { |
| 6 | int s = add(2, 3); |
| 7 | printf("%d\n", s); |
| 8 | return 0; |
| 9 | } |
| 10 | |
| 11 | int add(int a, int b) { |
| 12 | return a + b; |
| 13 | } |
2. 拆解
- 先声明后定义,调用点就能通过编译类型检查
- 参数 a,b 是 main 传入值的副本
- return 把结果交回调用者
3. 多文件预告
工程上:声明放 mymath.h,实现放 mymath.c,主文件 include。现在单文件写熟即可。