一、函数的概念
二、函数间的调度关系
1、函数分为两类
#include<stdio.h>
void main(){
int a,b,c;
int max(int x,int y); //调用max函数
scanf("%d,%d",&a,&b);
c=max(a,b);
printf("最大的数为=%d\n",c);
}
int max(int x,int y){ //定义max函数
int z;
z = x>y ? x:y ;
return z;
}
2、形参和实参的说明
三、函数的声明和定义
如果函数的定义出现在main()函数之前,则可以不必要加上声明。因为编译系统已经知道了已定义函数的有关情况。
四、函数间的嵌套
main()—>a()—>b()—>a()—>main()。即使a里没有return也会返回。
从main()开始也从main()结束。
五、算法——递归
递归必须要有有关判断条件用来作退出语句。
递归的操作路线类似于:知识点四
#include<stdio.h>
long func(int a);
void main(){
int a,d;
printf("输入数字\n");
scanf("%d",&a);
d=func(a);
printf("%d\n",d);
}
long func(int a){
int b;
if(a<0){
printf("输入错误");
}else if(a==0 || a==1){
b =1;
}else{
b=func(a-1)*a;
}
return b;
}
六、static声明局部变量
#include<stdio.h>
int f(int a ){
auto int b=0;
static int c =3;
b = b + 1; // 1 , 1 , 1
c = c + 1; // 4 , 5 , 6
return a+b+c; // 7 , 8 , 9
}
void main(){
int a=2,i;
for(i=0;i<3;i++){
printf("%d\n",f(a));
}
}
七、register变量
1、register存在的意义:
如果有些变量使用频繁(例如在一个函数中执行10000次循环,每一次循环中都要引用某局部变量),则为存取变量的值要花费不少的时间!
八、extern声明全局变量
#include<stdio.h>
int max(int A,int B){
int z;
z =x>y ? x:y;
return z;
}
void main(){
extern A,B; //试试去掉extern关键字
printf("%d\n",max(A,B));
}
int A=13,B=-8;
因为int A=13,B=-8; 写在了main的下面且编译器是从上往下读的,所以要用extern 。如果把int A=13,B=-8; 放在main之前就可以不用extern 关键字。