Contents
  1. Default Function Arguments
  2. Function Placeholder Parameters
  3. Function Overloading
  4. 1. Overview of Function Overloading
  5. 2. Notes on Function Overloading

Default Function Arguments

In C++, parameters in a function’s parameter list can have default values.

Syntax: 返回值类型 函数名 (参数=默认值){}

int func(int a, int b = 20, int c = 30){
    return a + b + c;
}

If an argument is passed to a parameter, the supplied value is used; otherwise, the default value is used.

Notes:

  • If a parameter at a given position has a default argument, every parameter after it must also have a default value. (In other words, parameters with default arguments must come last.)

    //错误示例:
    int func(int a, int b = 10, int c){
        return a + b + c;
    }
  • If the function declaration has default arguments, the function definition cannot also have default arguments. (In other words, only the declaration or the definition can have default arguments.)

    //错误示例:
    int func(int a = 10, int b = 10);
    
    int func(int a = 10, int b = 10){
        return a + b;
    }
    error C2572: "func2":重定义默认参数

Function Placeholder Parameters

In C++, a function’s parameter list can contain placeholder parameters. They occupy a position, and an argument must be supplied for that position when the function is called.

Syntax: 返回值类型 函数名 (数据类型){}

void func(int a,int){
    cout << "This is a function." << endl;
}

int main(){
    func(10, 10);	//这里必须要传两个数据
    return 0;
}

Placeholder parameters can also have default arguments.

void func(int a, int = 10){
    cout << "This is a function." <<endl
}

int main(){
    func(10);
    return 0;
}

Function Overloading

1. Overview of Function Overloading

Purpose: The same function name can be reused, improving reusability.

Conditions for function overloading

  • Within the same scope
  • The function names are the same
  • The function parameters have different types, counts, or orders
//例:
//func函数都在全局作用域下
//func函数名称相同
//func函数参数类型不同、或个数不同、或顺序不同
void func(){
    cout << "This is func()." << endl;
}

void func(int a){
    cout << "This is func(int a)." << endl;
}

void func(double a){
    cout << "This is func(double a)." << endl;
}

void func(int a, double b){
    cout << "This is func(int a, double b)." << endl;
}

void func(double a, int b){
    cout << "This is func(double a, int b)." << endl;
}

int main(){
    func();
    func(10);
    func(3.14);
    func(10,3.14);
    func(3.14,10);
    return 0;
}

2. Notes on Function Overloading

  • A function’s return value cannot be used as a condition for function overloading.

    //错误示例
    //无法重载仅按返回类型区分的函数
    void func(double a, int b){
        cout << "This is func(double a, int b)." << endl;
    }
    
    int func(double a, int b){
        cout << "This is func(double a, int b)." << endl;
    }
  • References as an overloading condition

    //两个函数类型不同
    void func(int &a){
        cout << "This is func(int &a)." <<endl;
    }
    
    void func(const int &a){
        cout << "This is func(const int &a)." <<endl;
    }
    
    int main(){
        int a = 10;
        func(a);	//调用func(int &a)
        func(10);	//调用func(const int &a)
        return 0;
    }
  • Function overloading with default arguments

    void func(int a){
        cout << "This is func(int a)." <<endl;
    }
    
    void func(int a, int b = 10){
        cout << "This is func(int a, int b)." <<endl;
    }
    
    int main(){
        func(10);	//X 当函数重载碰到默认参数,出现二义性报错,尽量避免重载时使用默认参数
        func(10, 20);	//√
    }

    Reference: A Meticulously Crafted Course by Heima Programmer | C++ Tutorial: Start at 0 and Advance Through 1, Making Programming Easier to Learn Link: https://www.bilibili.com/video/BV1et411b73Z