变量的引用作为函数参数的示例
1
//把变量的引用作为函数形参数
2
#include<iostream>
3![]()
4
using namespace std;
5![]()
6
int main()
7
{
8
int x,y;
9![]()
10
void SWAP(int &num1,int &num2);//函数原形
11![]()
12
cout<<"请输入两个数字:";
13
cin>>x>>y;
14![]()
15
SWAP(x,y);
16![]()
17
cout<<"交换结果:"<<" x="<<x<<" y="<<y;
18
}
19![]()
20
void SWAP(int &num1,int &num2)//参数为引用
21
{
22
int Exc;
23![]()
24
Exc=num1;
25
num1=num2;
26
num2=Exc;
27
}
28![]()
29![]()
//把变量的引用作为函数形参数2
#include<iostream>3

4
using namespace std;5

6
int main()7
{8
int x,y;9

10
void SWAP(int &num1,int &num2);//函数原形11

12
cout<<"请输入两个数字:";13
cin>>x>>y;14

15
SWAP(x,y);16

17
cout<<"交换结果:"<<" x="<<x<<" y="<<y;18
}19

20
void SWAP(int &num1,int &num2)//参数为引用21
{22
int Exc;23

24
Exc=num1;25
num1=num2;26
num2=Exc;27
}28

29



浙公网安备 33010602011771号