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

函数的引用

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;
}
相关推荐
草莓熊Lotso43 分钟前
MySQL 从入门到实战:视图特性 + 用户权限管理全解
linux·运维·服务器·数据库·c++·mysql
雾岛听蓝44 分钟前
进程信号机制深度解析
linux·开发语言·经验分享·笔记
Q741_14744 分钟前
每日一题 力扣 1848. 到目标元素的最小距离 模拟 C++题解
c++·算法·leetcode·模拟
VkN2X2X4b2 小时前
算法性能的渐近与非渐近行为对比的技术9
算法
好家伙VCC2 小时前
**神经编码新视角:用Python实现生物启发的神经信号压缩与解码算法**在人工智能飞速发展的今天
java·人工智能·python·算法
踏着七彩祥云的小丑8 小时前
pytest——Mark标记
开发语言·python·pytest
Dream of maid8 小时前
Python12(网络编程)
开发语言·网络·php
W23035765739 小时前
经典算法:最长上升子序列(LIS)深度解析 C++ 实现
开发语言·c++·算法
.Ashy.9 小时前
2026.4.11 蓝桥杯软件类C/C++ G组山东省赛 小记
c语言·c++·蓝桥杯
Y4090019 小时前
【多线程】线程安全(1)
java·开发语言·jvm