1,函数声明(.h)不要指定形参的默认值,在函数定义中指定。
2,指定了一个形参的默认值,后面的形参全都要指定默认值。
3,调用函数时,如果未传递参数的值,则会使用默认值,如果指定了值,则会忽略默认值,使用传递的值。如果实际参数的值留空,则使用这个默认值。
#include <iostream> using namespace std; int sum(int a, int b=20) { int result; result = a + b; return (result); } int main () { // 局部变量声明 int a = 100; int b = 200; int result; // 调用函数来添加值 result = sum(a, b); cout << "Total value is :" << result << endl; // 再次调用函数 result = sum(a); cout << "Total value is :" << result << endl; return 0; }