- 默认参数:当没有实参时,默认的值。
- 当函数有一个参数为默认参数,那么从这个参数起,后面的参数都必须有默认参数。
- 在函数的声明和定义中,默认参数只能写一次,不然编译器会报错,特别是在分文件编写中
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//1.默认参数
void test01(int a = 10, int b = 10)
{
cout << "a+b=" << a + b << endl;
}
//1.1当函数有一个参数为默认参数,那么从这个参数起,后面的参数都必须有默认参数。
void test02(int a, int b = 10, int c = 10)
{
cout << "a+b+c=" << a + b + c << endl;
}
//1.2 在函数的声明和定义中,默认参数只能写一次,不然编译器会报错,特别是在分文件编写中。
void test03(int a = 10, int b = 10);
void test03(int a, int b)
{
cout << "a+b=" << a + b << endl;
}
int main()
{
//test01();
test02(10);
system("pause");
return EXIT_SUCCESS;
}
2.占位参数:占位参数必须填入实参,占位参数也可以有默认值
//2.占位参数 暂时未发现其作用
void test04(int a, int)
{
}
void test05(int a, int = 10)
{
}
int main()
{
//test01();
//test02(10);
//test04(12,1);
test05(12);
system("pause");
return EXIT_SUCCESS;
}