码迷,mamicode.com
首页 > 其他好文 > 详细

21)函数的默认参数和占位参数

时间:2018-01-02 23:24:29      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:printf   定义函数   post   str   print   body   c++   clu   class   

1)自定义函数有默认参数,可以传参,也可以不传参

  

 1 #include<iostream>
 2 int fun(int a=100)
 3 {
 4     printf("%d\n",a);
 5 }
 6 int main()
 7 {
 8     fun();//这种是  我没有传参数
 9 
10     printf("\n");
11 
12     fun(10);//这种是我传了参数
13 
14     return 0;
15 
16 
17 }

2)你给函数的默认参数,只能从最右面开始

1 int  fun(int  a,int b=100,int c)
2 
3 //上面这中写法是不行的
4 
5 //下面的可以
6 
7 int  fun(int  a,int  b,int c=100int d=10

3)针对(2)的正确写法的函数,我传参时,有几种写法

1 fun(1020//这个是a=10,b=20,c=100,d=10
2 
3 fun(102030//这个是a=10,b=20,c=30,d=10
4 
5 fun(102030100//这个是a=10,b=20,c=30,d=100
6 
7 哈哈,在PHP中,可以
8 fun(1020,d=100)  但是在C++中,不可以这么干

21)函数的默认参数和占位参数

标签:printf   定义函数   post   str   print   body   c++   clu   class   

原文地址:https://www.cnblogs.com/xiaoyoucai/p/8179140.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!