在C++中,可以为参数指定默认值,C语言是不支持默认参数的,Java也不支持!!!
默认参数的语法与使用:
(1)在函数声明或定义时,直接对参数赋值。这就是默认参数;
(2)在函数调用时,省略部分或全部参数。这时可以用默认参数来代替。
注意事项:
(1)函数默认值只能赋值一次,或者是在声明中,或者是在定义中,如下所示
1. /*正确*/
2. #include <iostream>
3. int f(int a=5);
4. int f(int a)
5. {
6. std::cout <<a<<std::endl;
7. return a;
8. }
9. int main()
10. {
11. f();
12. return 0;
13. }
14.
15. /*正确*/
16. #include <iostream>
17. int f(int a=5)
18. {
19. std::cout <<a<<std::endl;
20. return a;
21. }
22. int main()
23. {
24. f();
25. return 0;
26. }
27.
28. /*正确*/
29. #include <iostream>
30. int f(int a);
31. int f(int a=5)
32. {
33. std::cout <<a<<std::endl;
34. return a;
35. }
36. int main()
37. {
38. f();
39. return 0;
40. }
41.
42.
43. /*错误*/
44. #include <iostream>
45. int f(int a=5);
46. int f(int a=5)
47. {
48. std::cout <<a<<std::endl;
49. return a;
50. }
51. int main()
52. {
53. f();
54. return 0;
55. }
56.
57. [niuxinli@localhost ~]$ make test
58. g++ test.cpp -o test
59. test.cpp: In function ‘int f(int)’:
60. test.cpp:3: error: default argument given for parameter 1 of ‘int f(int)’
61. test.cpp:2: error: after previous specification in ‘int f(int)’
62. 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呢?所以必须做这个限定。
1. #include <iostream>
2. int f(int a,int b);
3. int f(int a=5,int b)
4. {
5. std::cout <<a<<std::endl;
6. return a;
7. }
8. int main()
9. {
10. f(6);
11. return 0;
12. }
13.
14. g++ test.cpp -o test
15. test.cpp: In function ‘int f(int, int)’:
16. test.cpp:3: error: default argument missing for parameter 2 of ‘int f(int, int)’
17. 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)默认参数可以是全局变量,全局常量,还可以是函数,但是不能是局部变量,因为局部变量在编译时未定
如
1. [niuxinli@localhost ~]$ cat test.cpp
2. #include <iostream>
3. int x = 5;
4. int f(int a,int b,int c);
5. int f(int a,int b=5,int c=x)
6. {
7. std::cout <<a+b+c<<std::endl;
8. return a;
9. }
10. int f2(int (*func)(int,int,int)=f )
11. {
12. func(2,3,5);
13. return 0;
14. }
15. int main()
16. {
17. f(1);
18. f2();
19. return 0;
20. }
21.
22. [niuxinli@localhost ~]$ make test && ./test
23. g++ test.cpp -o test
24. 11
25. 10
但是注意一点,func不能使用默认参数了,因为func是局部变量,它是后来被赋值成f的
1. [niuxinli@localhost ~]$ cat test.cpp
2. #include <iostream>
3. int x = 5;
4. int f(int a,int b,int c);
5. int f(int a,int b=5,int c=x)
6. {
7. std::cout <<a+b+c<<std::endl;
8. return a;
9. }
10. int f2(int (*func)(int,int,int)=f )
11. {
12. func(2);
13. return 0;
14. }
15. int main()
16. {
17. f(1);
18. f2();
19. return 0;
20. }
21. [niuxinli@localhost ~]$ make test
22. g++ test.cpp -o test
23. test.cpp: In function ‘int f2(int (*)(int, int, int))’:
24. test.cpp:11: error: too few arguments to function
25. make: *** [test] Error 1