//函数默认参数 //在C++中,函数的形参列表中的形参是可以有默认值的。 //语法 : 返回值类型 函数名 (参数 = 默认值) {} #if(0) #include //如果我们自己传入数据,就用自己的数据,如果没有,那么就用默认值 //语法 : 返回值类型 函数名 (形参 = 默认值) int func(int a, int b = 20, int c = 30) { return a + b + c; } //注意事项 //1、如果某个位置已经有了默认参数,那么从这个位置往后,从左到右必须都有默认值; //int func2(int a, int b = 10, int c, int d) //{ // return a + b + c; //} //2、如果函数的声明有了默认,那么函数的实现就不能有默认参数 //声明和实现只能有一个有默认参数 int func2(int a , int b ); int func2(int a = 10, int b = 10) { return a + b; } int main() { std::cout << func(10) << std::endl; std::cout << func2(10, 10) << std::endl; system("pause"); return 0; } #endif