#include<iostream>
using namespace std;
void displayInfo(const int& num)
{
// 函数体内不能修改num的值
cout << "num = " << num << endl;
}
int main()
{
int myNumber = 5;
displayInfo(myNumber);// 传递myNumber的引用,但不允许修改
system("pause");
return 0;
}
如 displayInfo(const int& num)函数体内对num进行了修改,会报错
#include<iostream>
using namespace std;
// 位置b有默认值,那么b之后的参数都要有默认值
int func(int a, int b = 10, int c = 10)
{
return a + b + c;
}
int func2(int a = 10, int b = 10);
// 声明和实现只能一个有默认参数
// 函数被定义两次是不允许的
//int func2(int a = 20 , int b = 30)
int func2(int a, int b)
{
return a + b;
}
int main()
{
cout<<func(10)<<endl;
cout << func2() << endl;
}