函数-值传递

  • 值传递

 

点击查看代码
#include<iostream>
#include<string>

using namespace std;

//值传递
//定义函数,实现两个数字进行交换函数
void swap(int num1, int num2)
{
	cout << "交换前:" << endl;
  	cout << "num1 = " << num1 << endl;
  	cout << "num2 = " << num2 << endl;
  
  	int tmp = num1;
  	num1 = num2;
  	num2 = tmp;
  
  	cout << "交换后:" << endl;
  	cout << "num1 = " << num1 << endl;
  	cout << "num2 = " << num2 << endl;
}

int main(){

	int a = 10;
  	int b = 20;
  	swap(a, b);
	
  	//注意
    //当我们做值传递的时候,函数的形参发生改变,并不会影响实参	
  	cout << "a = " << a << endl;
  	cout << "b = " << b << endl;
  
  

	system("pause");

	return 0;
}

 

posted @ 2021-07-20 14:41  毋纵年华  阅读(53)  评论(0)    收藏  举报