一、函数的形参
1、形参
- 函数的形参可以有默认值
- 如果声明有默认参数,则实现就不能有默认参数。声明和实现只能由一个有默认参数。
2、函数占位参数:用来占位,可以只写参数的类型,不写变量名
#include<iostream>
using namespace std;
// 第二个形参是占位参数,只写了数据类型,无变量。第三个形参是带默认形参的占位参数
void Test(int a,int,int =10)
{
cout << "This is Test funcation " << endl;
}
int main()
{
int s = 10;
Test(10,20);
cin.get();
}
二、函数的重载:
1、函数重载的基本要求:
- 函数名相同
- 同一个作用域下
- 函数参数类型不同、或参数个数不同
2、常引用和引用是函数的形参类型不同
#include<iostream>
using namespace std;
void Test(int &a)
{
cout << "This is :int &a " << endl;
}
void Test(const int &a)
{
cout << "This is :const int &a " << endl;
}
int main()
{
int s = 10;
Test(s); // 调用第一个 Test函数,s为变量
Test(20);// 调用第二个Test函数。int &a=20 是不合法的。const int &a=20 合法
cin.get();
}