实参与形参类型不匹配时,实参自动转化为形参类型
return语句 返回数据的类型与函数类型不匹配时,自动转化为函数的类型
函数声明 事先通知编译系统,可以不写形参名
example:float add(float)
技巧
编程时把main函数写到最前面,每次需调用函数时都要写函数原型来声明函数
example:void swap(int,int)
参数
指针作为函数参数
声明 void swap(int *p1,int *p2)
调用swap(pointer_1,pointer_2)
一维数组地址作函数参数,用指针变量去接收或者用数组接收
声明:void swap(int a[]); void swap(int* a);
调用:swap(a),//a为数组名
多维数组名用作函数参数时,用指向一维数组的指针变量接收或多维数组接受
声明:void swap(int a[][n]); void swap(int(*p)[n]);
//n为具体数组的列数,必须要指定
调用:swap(a);
二维数组a[5][6]; a=&a[0] a[0]=&a[0][0]
a[1]=&a[1][0] a[2]==&a[2][0]…//数组名为首元
函数指针做参数
声明:void swap(double(*p)(int,int));
调用:swap(f);//只需传参数名(函数入口地址)
void *swap(int ,int )//声明一个函数swap,返回指向void类型的指针
void (*swap)(int,int)声明一个指向函数的指针,这个函数是swap
引用作为函数参数
声明:void swap(int &,int &)
调用:swap(i,j)//i,j可以是变量也可以是引用类型
结构体类型变量作参数
声明:void swap(struct student stu1);
调用:swap(std);//stu是结构提student的一个变量
类对象作参数
声明:void swap(student stu1)
调用:swap(stu)//stu是类student一个对象
定义字符指针
char* str=“I Love China”//str是指针,系统将首元素I的地址给str。当然字符串后面自动加’\0’