C++学习第三十四篇

/*
* 值传递:
* 所谓值传递,就是函数调用时实参将数值传入给形参
* 值传递时,如果形参发生改变,并不会影响实参
*/
#include<iostream>
using namespace std;

//定义一个函数,实现两个数进行交换
//如果函数不需要返回值,声明的时候可以用void
void exch(int num1,int num2)
{
	cout << "交换前:" << endl;
	cout << "num1 = " << num1 << endl;
	cout << "num2 = " << num2 << endl;
	int temp = num1;
	num1 = num2;
	num2 = temp;
	cout << "交换后:" << endl;
	cout << "num1 = " << num1 << endl;
	cout << "num2 = " << num2 << endl;
	//return;返回值不需要时,可以不写return
}

int main()
{
	int a = 10;
	int b = 20;
	exch(a, b);
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	system("pause");
	return 0;
}

			    

猜你喜欢

转载自blog.csdn.net/weixin_47358937/article/details/121505463