默认参数这个特性在C中是没有的

    • A feature in C++ (not C).
    • To call a function without providing one or more trailing arguments. ```cpp float norm(float x, float y, float z); float norm(float x, float y = 0, float z);

    // float norm(float x, float y, float z = 0); // Parameter-list of a function declaration. // 0是argument,z是parameter

    int main() { cout << norm(3.0f) << endl; cout << norm(3.0f, 4.0f) << endl; cout << norm(3.0f, 4.0f, 5.0f) << endl; return 0; }

    float norm(float x, float y, float z) { return sqrt(x x + y y + z * z); }

    // 3 // 5 // 7.07107 ``` 在函数声明中,我们可以在函数的参数中设置默认值,如果没有传进参数,函数就使用默认参数。
    默认参数只可放在不默认参数的后面,将第四行代码注释掉后,运行代码会报错
    image.png
    默认参数的好处:
    如果函数的参数列表非常长,同时函数调用时,参数的值都差不多,那么就可以使用默认参数,函数调用的时候,就只需填写需要使用的参数,方便调用。