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

函数的引用

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;
}
相关推荐
*.✧屠苏隐遥(ノ◕ヮ◕)ノ*.✧13 分钟前
C语言_数据结构总结9:树的基础知识介绍
c语言·开发语言·数据结构·b树·算法·visualstudio·visual studio
好看资源平台13 分钟前
加密算法逆向与HOOK技术实战
开发语言·python
byxdaz16 分钟前
QT编程之QGIS
开发语言·qt
烂蜻蜓20 分钟前
深入理解 HTML 中的<div>和元素:构建网页结构与样式的基石
开发语言·前端·css·html·html5
Honeysea_701 小时前
常用的Python库
开发语言·python·机器学习·计算机视觉·ai·自然语言处理
编程梦想记1 小时前
Python在数据处理中的应用:从入门到精通
开发语言·python·信息可视化
字节源流1 小时前
【SpringMVC】常用注解:@PathVariable
java·开发语言·servlet
小安同学iter1 小时前
SpringMVC(五)拦截器
java·开发语言·spring boot·spring·java-ee
攻城狮7号1 小时前
【第九节】windows sdk编程:通用控件的使用
c++·windows·windows编程·windows sdk
Dann Hiroaki1 小时前
文献分享: 对ColBERT段落多向量的剪枝——基于学习的方法
学习·算法·剪枝