温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

21天学通C++(1-1)

发布时间:2020-06-11 18:55:18 阅读:2473 作者:nxlhero 栏目:移动开发
C++开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

函数默认参数

在C++中,可以为参数指定默认值,C语言是不支持默认参数的,Java也不支持!!!

默认参数的语法与使用:
(1)在函数声明或定义时,直接对参数赋值。这就是默认参数;
(2)在函数调用时,省略部分或全部参数。这时可以用默认参数来代替。

注意事项:

(1)函数默认值只能赋值一次,或者是在声明中,或者是在定义中,如下所示

/*正确*/  #include <iostream> int f(int a=5);  int f(int a)  {          std::cout <<a<<std::endl;          return a;  }  int main()  {          f();          return 0;  }   /*正确*/  #include <iostream> int f(int a=5)  {          std::cout <<a<<std::endl;          return a;  }  int main()  {          f();          return 0;  }   /*正确*/  #include <iostream> int f(int a);  int f(int a=5)  {          std::cout <<a<<std::endl;          return a;  }  int main()  {          f();          return 0;  }    /*错误*/  #include <iostream> int f(int a=5);  int f(int a=5)  {          std::cout <<a<<std::endl;          return a;  }  int main()  {          f();          return 0;  }   [niuxinli@localhost ~]$ make test  g++     test.cpp   -o test  test.cpp: In function ‘int f(int)’:  test.cpp:3: error: default argument given for parameter 1 of ‘int f(int)’  test.cpp:2: error: after previous specification in ‘int f(int)’  make: *** [test] Error 1  

(2) 默认参数定义的顺序为自右到左。即如果一个参数设定了缺省值时,其右边的参数都要有缺省值。比如int f(int a, int b=1,int c=2,int d=3)是对的,但是int f(int a,int b=1,int c=2,int d)就是错的。这个的原因很显然,你传几个参数,编译器都认为是从左向右的,比如int f(int a,int b=1,int c),传入了f(1,2),它会认为a=1,b=2,那c呢?所以必须做这个限定。

#include <iostream>  int f(int a,int b);  int f(int a=5,int b)  {          std::cout <<a<<std::endl;          return a;  }  int main()  {          f(6);          return 0;  }   g++     test.cpp   -o test  test.cpp: In function ‘int f(intint)’:  test.cpp:3errordefault argument missing for parameter 2 of ‘int f(intint)’  make: *** [test] Error 1  

(3)默认参数调用时,则遵循参数调用顺序,自左到右逐个调用。这一点要与第(2)分清楚,不要混淆。

如:void mal(int a, int b=3, int c=5); //默认参数
mal(3, 8, 9 ); //调用时有指定参数,则不使用默认参数
mal(3, 5); //调用时只指定两个参数,按从左到右顺序调用,相当于mal(3,5,5);
mal(5); //调用时只指定1个参数,按从左到右顺序调用v当于mal(5,3,5);
mal( ); //错误,因为a没有默认值
mal(3, , 9) //错误,应按从左到右顺序逐个调用

(4)默认参数可以是全局变量,全局常量,还可以是函数,但是不能是局部变量,因为局部变量在编译时未定

[niuxinli@localhost ~]$ cat test.cpp  #include <iostream>  int x = 5;  int f(int a,int b,int c);  int f(int a,int b=5,int c=x)  {          std::cout <<a+b+c<<std::endl;          return a;  }  int f2(int (*func)(int,int,int)=f )  {      func(2,3,5);      return 0;  }  int main()  {          f(1);          f2();          return 0;  }   [niuxinli@localhost ~]$ make test && ./test  g++     test.cpp   -o test  11  10  

但是注意一点,func不能使用默认参数了,因为func是局部变量,它是后来被赋值成f的

[niuxinli@localhost ~]$ cat test.cpp  #include <iostream>  int x = 5;  int f(int a,int b,int c);  int f(int a,int b=5,int c=x)  {          std::cout <<a+b+c<<std::endl;          return a;  }  int f2(int (*func)(int,int,int)=f )  {      func(2);      return 0;  }  int main()  {          f(1);          f2();          return 0;  }  [niuxinli@localhost ~]$ make test  g++     test.cpp   -o test  test.cpp: In function ‘int f2(int (*)(intintint))’:  test.cpp:11error: too few arguments to function  make: *** [test] Error 1  

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×