函数的引用/函数的默认参数/函数的占位参数/函数重载

函数的引用

cpp 复制代码
#include<iostream>
using namespace std;

//引用的本质在c++内部实现,是一个指针常量

//交换函数
//1.值传递
void mySwap01(int a, int b) {
	int temp = a;
	a = b;
	b = temp;
}

//2.地址传递
void mySwap02(int *a, int *b) {
	int temp = *a;
	*a = *b;
	*b = temp;
}
//3.引用传递
void mySwap03(int& a, int& b) {
	int temp = a;
	a = b;
	b = temp;
}


//1.不要返回局部变量的引用
int& test01() {
	int a = 10;//局部变量,存放在栈
	return a;
}

//2.函数的调用可以作为左值
int& test02() {
	static int a = 10;//存放在全局区,在程序结束后系统释放
	return a;
}


//打印数据函数
void showValue(const int& value) {
	//value = 1000;  防止这种误操作
	cout << "value=" << value << endl;
}

int main() {
	int a = 10;
	//创建引用------起别名------别名的前面加上&------操作的都是同一块内存
	int& c = a;//引用必须要初始化;引用一旦初始化后,就不可以更改了
	c = 20;
	cout << "a=" << a << endl;
	cout << "------------------" << endl;


	//引用做函数参数
	a = 10;
	int b = 20;
	mySwap01(a, b);//值传递,形参不会修饰实参
	cout << "值传递  a=" << a << " b=" << b << endl;//a=10 b=20
	cout << "------------------" << endl;

	mySwap02(&a, &b);
	cout << "地址传递  a=" << a << " b=" << b << endl;
	cout << "------------------" << endl;

	mySwap03(a, b);
	cout << "引用传递  a=" << a << " b=" << b << endl;
	cout << "------------------" << endl;


	//引用做函数的返回值
	int& ref1 = test01();
	cout << "ref1=" << ref1 << endl;
	//cout << "ref1=" << ref1 << endl;

	int& ref2 = test02();
	cout << "ref2=" << ref2 << endl;
	cout << "------------------" << endl;


	//常量引用  const  用来修饰形参防止误操作
	
	const int& ref = 10;//引用必须引一块合法的内存空间

	a = 100;
	showValue(a);
	

	//system("pause");
	return 0;
}

函数的默认参数

cpp 复制代码
#include<iostream>
using namespace std;

//函数的默认参数

//如果自己传入数据,就用自己的数据,如果没有就用默认值
//语法:返回值类型 函数名(形参=默认值){}
int func(int a, int b=20, int c=30) {
	return a + b + c;
}


//1.如果一个位置有了默认参数,那么从这个位置开始,从左到右都要有默认值
//2.如果函数声明有默认参数,函数实现就不能有默认参数;是现有声明就别有
int main() {
	cout << func(10) << endl;
	//system("pause");
	return 0;
}

函数的占位参数

cpp 复制代码
#include<iostream>
using namespace std;

//占位参数
//返回值类型 函数名 (数据类型){}
//占位参数 还可以有默认参数
void func(int a, int =10) {//int 是占位用的
	cout << "this is func" << endl;
}
int main() {
	func(10);
	return 0;
}

函数重载

cpp 复制代码
#include<iostream>
using namespace std;
//函数重载
//可以让函数名相同,提高复用性

/*
函数重载的满足条件
1.同一个作用域下
2.函数名相同
3.参数类型不同,或个数不同,或顺序不同

注意:函数的返回值不可以作为函数重载的条件
*/
void func(int a) {
	cout << "func的调用" << endl;
}

void func() {
	cout << "func的调用!" << endl;
}
int main() {
	func();
	func(10);
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
//函数重载
//可以让函数名相同,提高复用性

/*
函数重载的满足条件
1.同一个作用域下
2.函数名相同
3.参数类型不同,或个数不同,或顺序不同

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

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

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


//函数重载的注意事项
//1.引用作为重载的条件

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



int main() {

	func();
	func(10);

	int a = 30;
	fun(a);//a可变
	fun(40);//40不可变
	return 0;
}
相关推荐
止观止22 分钟前
JavaScript对象创建9大核心技术解析
开发语言·javascript·ecmascript
AA陈超1 小时前
虚幻引擎UE5专用服务器游戏开发-20 添加基础能力类与连招能力
c++·游戏·ue5·游戏引擎·虚幻
mit6.8242 小时前
[Meetily后端框架] AI摘要结构化 | `SummaryResponse`模型 | Pydantic库 | vs marshmallow库
c++·人工智能·后端
screenCui2 小时前
macOS运行python程序遇libiomp5.dylib库冲突错误解决方案
开发语言·python·macos
R-G-B2 小时前
【02】MFC入门到精通——MFC 手动添加创建新的对话框模板
c++·mfc·mfc 手动添加创建新的对话框
linux kernel2 小时前
第七讲:C++中的string类
开发语言·c++
jz_ddk2 小时前
[实战]调频(FM)和调幅(AM)信号生成(完整C语言实现)
c语言·算法·信号处理
Tipriest_2 小时前
[数据结构与算法] 优先队列 | 最小堆 C++
c++·优先队列·数据结构与算法·最小堆
玩代码2 小时前
Java线程池原理概述
java·开发语言·线程池
CloudAce云一2 小时前
谷歌云代理商:谷歌云TPU/GPU如何加速您的AI模型训练和推理
算法