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; }