做网站的软件叫81什么来着,徐州网站备案,工业互联网龙头公司排名,微信如何做积分商城网站师从黑马程序员
函数默认参数
在C中#xff0c;函数的形参列表中的形参是可以有默认值的
语法#xff1a;返回值类型 函数名 #xff08;参数默认值 {}#xff09;
#include iostream
using namespace std;//函数默认参数//如果我们自己传入数据#xff0c;…师从黑马程序员
函数默认参数
在C中函数的形参列表中的形参是可以有默认值的
语法返回值类型 函数名 参数默认值 {}
#include iostream
using namespace std;//函数默认参数//如果我们自己传入数据就用自己的数据如果没有那么用默认值
//语法返回值类型 函数名形参默认值{}
int func(int a,int b20,int c30)
{return abc;
}//注意事项
//1、如果某个位置已经有了默认参数那么从这个位置往后从左到右必须有默认值
/*wrong
int fuc(int a10,int b,int c,int d)
{return abc;
}
*/
//2、如果函数声明有默认参数函数实现就不能有默认参数
//声明和实现只能有一个默认参数
int func2(int a10,int b10 );int func2(int a,int b )
{return ab;
}int main()
{coutfunc(10)endl;//60coutfunc2()endl;//20return 0;
}函数占位参数
C中函数的形参列表里可以有占位参数用来做占位调用函数时必须填补该位置 语法返回值类型 函数名数据类型{}
#include iostream
using namespace std;//占位参数
//返回值类型 函数名数据类型{}
//占位参数 还可以有默认参数
void func(int a,int 10)
{coutthis is funcendl;
}int main()
{func(10);return 0;
}函数重载
作用函数名可以相同提高复用性
函数重载满足条件
同一个作用域下
函数名称相同
函数参数类型不同或个数不同或顺序不同
注函数的返回值不可以作为函数重载的条件
#include iostream
using namespace std;//函数重载
//函数名可以相同提高复用性void func()
{coutfunc 的调用endl;
}
void func(int a)
{coutfunc(int a) 的调用endl;
}
void func(double a)
{coutfunc(double a) 的调用endl;
}
void func(int a,double b)
{coutfunc(int a,double b) 的调用endl;
}
void func(double a,int b)
{coutfunc(double a) 的调用endl;
}
/*不构成函数重载
int func(double a,int b)
{coutfunc(double a) 的调用endl;
}
*/
int main()
{func();func(10);func(3.14);func(10,3.14);func(3.14,10);return 0;
}函数重载注意事项
引用作为函数重载条件
函数重载碰到函数默认参数
#include iostream
using namespace std;
//1、引用作为函数重载条件
void func (int a)
{coutfunc(int a)调用endl;
}
void func (const int a)
{coutfunc(const int a)调用endl;
}
//2、函数重载碰到函数默认参数
void func(int a,int b10)
{coutfunc(int a,int b)的调用endl;
}void func(int a,)
{coutfunc(int a)的调用endl;
}
int main()
{int a10;func(a);//func(int a)调用func(10);//func(const int a)调用//func2 (10)//报错 碰到函数默认参数产生歧义需要避免return 0;
}若有侵权请联系作者