1.在c++中函数的形参可以有默认值

int func(int a,int b = 10){ }

  1. #include<iostream>
  2. using namespace std;
  3. int& test (int a,int b = 10,int c = 30){
  4. cout << a+b+c <<endl;
  5. }
  6. int main (void){
  7. test(10,20);
  8. }

如果传值,就用传值,否则用默认值
注意:1.如果某个值有了默认参数,从此位置的从左到右的参数都必须有默认值。
int func(int a,int b = 10,int c)错误
2.如果函数声明有了默认参数,那么函数实现不可以使用默认参数
eg:

int func (int a = 10,int b = 10);//声明中已经有了默认参数,不可以在函数实现中再写默认参数
int func (int a = 10,int  b =10){
...
}

错误:二义性

**

函数的占位参数:

可以在形参列表中写展位参数,调用时必须填补

#include<iostream>
using namespace std;
void test (int a,int){
    cout << "hello" <<endl; 
}
int main (void){

    test(10,10);

}

占位参数也可以有默认参数

函数重载:

函数名是可以相同的,提高复用性
条件:
1.同一个作用域下
2.函数名称相同
3.函数参数的类型不同或者个数不同或者顺序不同

函数的返回值不可以作为函数重载的条件**

#include<iostream>
using namespace std;
void test (int){
    cout << "this is func01" <<endl; 
}
void test (){
    cout << "this is func02" <<endl; 
}
int main (void){

    test(1);
    test();

}

函数重载:
引用作为函数重载的条件
const引用可以作为函数重载 的条件,例如:
int func(int &a){}
int func(const &c){}

int a = 10;
func (a); // 调用的是 int func(int &a){}
func (10); //调用的是 int func(const &c){}

函数重载碰到默认参数

void func(int a,int b = 10){
    cout << "this is func_1"
}
void func(int a,int b){
    cout << "this is func_2"
}
int mian(void){
    func(10);

}

当函数重载碰到默认参数导致调用产生二义性时,会导致报错。