三、C++函数提高

本文详细介绍了C++中函数的默认参数使用规则,包括参数顺序和声明实现的注意事项。接着讲解了函数占位参数的概念,允许函数形参列表中有未指定的参数,并可以通过默认值进行填充。最后讨论了函数重载的基本原则和避免歧义的方法,强调了返回值不能作为重载依据。通过示例代码帮助理解各个概念。
摘要由CSDN通过智能技术生成


一、函数的默认参数

简要说明

  • 在C++中,函数形参列表中的形参是可以有默认值的
  • 语法:返回类型 函数名 (形参 = 默认值){}

注意事项

1. 如果某个位置参数有默认值,那么从这参数往后,从左往右,都要有默认值

代码示例:

#include <iostream>
using namespace std;

int func(int a, int b = 10, int c = 20)
{
	return a + b + c;
}

int main()
{
	int ret;
	ret = func(10);
	cout << "ret = " << ret << endl;

	system("pause");
	return 0;
}

运行结果如下:

在这里插入图片描述


错误代码示例:

int func(int a, int b = 10, int c)    //c没有默认值,是错误的写法
{
	return a + b + c;
}


2. 如果函数声明有默认参数,那么函数实现就不能有默认参数

代码示例:

#include <iostream>
using namespace std;

int func(int a, int b = 20, int c = 10);   //函数的声明

int main()
{
	
	cout << "func(10) = " << func(10) << endl;

	system("pause");
	return 0;
}

int func(int a, int b, int c)      //函数的实现
{
	return a + b + c;
}

运行结果如下:

在这里插入图片描述

错误代码示例:

#include <iostream>
using namespace std;

int func(int a, int b = 10, int c = 20);   //函数的声明

int main()
{
	int ret;
	ret = func(10);
	cout << "ret = " << ret << endl;

	system("pause");
	return 0;
}

int func(int a, int b = 10, int c = 10)      //函数的实现
{
	return a + b + c;
}

在这里插入图片描述
如图所示,就算函数声明和函数实现处的默认值一样,生成时也会出错,所以,只需在函数声明处设置默认值即可。

3. 如果我们自己传入数据,编译器就用我们传入的数据,如果没有,就用默认值

代码示例:

#include <iostream>
using namespace std;

int func(int a, int b = 20, int c = 10);   //函数的声明

int main()
{
	
	cout << "func(10,30) = " << func(10,30) << endl;

	system("pause");
	return 0;
}

int func(int a, int b, int c)      //函数的实现
{
	return a + b + c;
}

运行结果如下:

在这里插入图片描述
10 + 30 + 10 = 50; 由运行结果可知编译器用的是我们传入的值。所以,如果我们自己传入数据,编译器就用我们传入的数据,如果没有,就用默认值

二、函数占位参数

简要说明

  • C++中,函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置
  • 语法: 返回值类型 函数名 (数据类型){}

代码如下(示例):

#include <iostream>
using namespace std;

void func(int a, int)     //第二个int为占位参数
{
	cout << "This is func." << endl;
}

int main()
{
	func(10,10);   //占位参数必须填补

	system("pause");
	return 0;
}
  • 占位参数也可以有默认参数

代码如下(示例):

#include <iostream>
using namespace std;

void func(int a, int = 10)     //第二个int为占位参数
{
	cout << "This is func." << endl;
}

int main()
{
	func(10);   //此时占位参数就不用填补了,因为她已经有默认值了

	system("pause");
	return 0;
}

现阶段看起来占位参数意义不大,后续会用到该项技术的。

三、函数重载

基本语法

作用:函数名可以相同,提高复用性

函数重载满足条件:

  • 同一个作用域下
  • 函数名称相同
  • 函数 参数类型不同 或者 个数不同 或者 顺序不同

注意: 函数的返回值不可以作为函数重载的条件

注意事项

1. 引用作为重载的条件

代码如下(示例):

#include <iostream>
using namespace std;

void func(int& a) //int &a = 10;不合法  详见引用篇
{
	cout << "func(int& a)的调用" << endl;
}

void func(const int& a)    //const int &a = 10;合法
{
	cout << "fund(const int& a)的调用" << endl;
}

int main()
{
	func(10);

	system("pause");
	return 0;
}

运行结果如下:

在这里插入图片描述
或者:

代码如下(示例):

#include <iostream>
using namespace std;

void func(int& a) //int &a = a;合法  详见引用篇
{
	cout << "func(int& a)的调用" << endl;
}

void func(const int& a)    
{
	cout << "fund(const int& a)的调用" << endl;
}

int main()
{
	int x = 10;
	func(x);

	system("pause");
	return 0;
}

运行结果如下:

在这里插入图片描述

2. 函数重载碰到默认参数

代码如下(示例):

#include <iostream>
using namespace std;

void func(int a, int b = 10)
{
	cout << "func(int a,int b)的调用" << endl;
}

void func(int a)
{
	cout << "func(int a)的调用" << endl;
}

int main()
{
	func(10);  //碰到默认参数会产生歧义,需要避免

	system("pause");
	return 0;
}

在这里插入图片描述
由此可见,函数重载要注意避免默认参数,以免产生歧义。


总结

提示:本节内容主要为函数提高

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值