C++函数提高课

1.默认参数

函数形参列表中的形参可以具有默认值。

语法:返回值类型 函数名 (参数=默认值){}

注意事项:

①如果某个位置已经有了默认参数,那么从这个位置开始从左往右都必须有默认值

4b4f59cfcded4b07a56c997082304f37.png

②如果一个函数得声明中存在默认参数,那么函数实现就不能再有默认参数

0a4bb42b711241dfab23fb2d9d6981a5.png

贴个正常的代码:

#include <iostream>
using namespace std;

int add(int a, int b = 10, int c = 5)
{
	return a + b + c;
}

//注意事项:①如果某个位置已经有了默认参数,那么从这个位置开始从左往右都必须有默认值
//int minus(int a = 100, int b)
//{
//	return a - b;
//}

//②如果一个函数得声明中存在默认参数,那么函数实现就不能再有默认参数
//这是函数的声明
//int func(int a=1, int b=11);

//这是函数的实现
//int func(int a=1, int b=11)
//{ 
//	return a + b;
//}


int main()
{
	int result = 0;
	result = add(1);
	printf("result=%d;\r\n", result);
	result = add(1,11);
	printf("result=%d;\r\n", result);
	result = add(1,11,6);
	printf("result=%d;\r\n", result);

	//result = func(1,11);
	//printf("result=%d;\r\n", result);

	system("pause");
	return 0;
}

2.函数的占位参数

C++函数的形参列表中可以包含占位参数,调用函数时必须填补该位置。

语法:返回值类型   函数名 (数据类型){}

先只贴个代码,后面才用到。

#include <iostream>
using namespace std;

void func(int a, int)
{
	printf("Hello World!\r\n");
}

int main()
{
	func(1,2);



	system("pause");
	return 0;
}

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值