首页 > 其他分享 >54.函数参数传递的三种方式

54.函数参数传递的三种方式

时间:2023-02-09 15:45:39浏览次数:45  
标签:tmp 传递 int 54 void 参数传递 三种

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

//值传递
void swap(int a, int b)
{
	int tmp = a;
	a = b;
	b = tmp;
}

//指针传递
void swap2(int *a, int *b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

//引用传递
void swap3(int &a, int &b)//int &a=a,int &b=b;
{
	int tmp = a;
	a = b;
	b = tmp;
}

//打印
void myprint(int &a,int &b)
{
	cout << "a=" << a << " b=" << b << endl;
}

int main()
{
	int a = 10;
	int b = 20;
	swap(a, b);
	myprint(a, b);//10,20

	swap2(&a, &b);
	myprint(a, b);//20,10

	a = 10;
	b = 20;

	swap3(a, b);
	myprint(a, b);//20,10
	system("pause");
	return EXIT_SUCCESS;
}


标签:tmp,传递,int,54,void,参数传递,三种
From: https://www.cnblogs.com/codemagiciant/p/17105499.html

相关文章