目录
🍯1.3 缺省参数
缺省参数一共有两种类型:全缺省参数、半缺省参数;它的本质就是当不传参数进入函数时,就用函数定义时所设置的默认参数。
🥝1. 全缺省参数
1、有一个缺省参数时
void TestFunc(int a = 0) { cout << a << endl; } int main() { TestFunc(); //没有传参时,使用参数的默认值 TestFunc(1); //传具体参数时,使用传入的参数值 return 0; }
当只有一个默认参数时,当我们一个参数都不传递的时候,就使用默认参数;当我们传入了指定参数时,我们就使用指定参数。
2、有多个缺省参数时
void TestFunc(int a = 10, int b = 20, int c = 30) { cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; } int main() { TestFunc(); //没有传参时,使用参数的默认值 TestFunc(1); //传1个参数 TestFunc(1,2);