函数值传递的特点:值传递过程中即使形参改变也不会改变实参
没有返回值的函数用" void "定义
下面是一个实例:
cpp
#include<iostream>
using namespace std;
//值传递
//定义函数,实现两个数字进行交换函数
//如果函数不需要返回值,声明可以写成void
void swap(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;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
//当我们做值传递的时候,函数的形参发生任何改变不会影响实参
swap(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
输出结果为