缺省参数是声明或定义函数时为函数的参数指定一个默认值。在调用该函数时,如果没有指定实参则采用该 默认值,否则使用指定的实参。
#include <iostream> using namespace std; void fun(int a=10) { cout<<a<<endl; } int main() { fun(); //无参时采用默认值 fun(10); //传参时使用实参 }全缺省参数:
int main() { int a; int b; int c; } void fun(int a=10,intb=20,int c=30) { cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; cout<<"c="<<c<<endl; }
**半缺省参数 **:·
void fun(int a,intb=20,int c=30) { cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; cout<<"c="<<c<<endl; }1.半缺省参数必须从右到左依次给出,不能间隔
2.缺省参数不能在函数声明和函数定义处同时出现
//a.h void func(int a = 10); // a.c void func(int a = 20) // 注意:如果声明与定义位置同时出现,恰巧两个位置提供的值不同,那编译器就无法确定到底该用那个缺省值。3.缺省值必须是常量或者全局变量
4.C语言不支持(编译器不支持)
、