myref.cpp

来自「Visual C++高级编程及其项目应用开发(含源代码)」· C++ 代码 · 共 40 行

CPP
40
字号
#include <iostream>
using namespace std;

void MySwap(int, int);
void MyRswap(int &, int &);

void main()
{
	int i,j;
	i = 20;
	j = 30;
	MySwap(i,j);	//这个函数按传值的方式传递参数
	cout<<"执行 MySwap 函数后i、j的值分别为: "<<endl
		<<"		i的值为: "<<i<<endl
		<<"		j的值为: "<<j<<endl;

	cout<<endl;

	MyRswap(i,j);	//这个函数按引用的方式传递参数
	cout<<"执行 MyRswap 函数后i、j的值分别为: "<<endl
		<<"		i的值为: "<<i<<endl
		<<"		j的值为: "<<j<<endl;
}

//定义函数
void MySwap(int a, int b)
{
	int temp;
	temp = b;
	b = a;
	a =temp;
}

void MyRswap(int &a, int &b)
{
	int temp;
	temp = b;
	b = a;
	a = temp;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?