1. #include <iostream>
    2. using namespace std;
    3. // 值传递
    4. // 定义函数 实现两个数字进行交换函数
    5. // 如果函数不需要返回值 void
    6. void swap(int num1, int num2) {
    7. cout << "交换前的数据 num1 = " << num1 << endl;
    8. cout << "交换前的数据 num2 = " << num2 << endl;
    9. int temp = num1;
    10. num1 = num2;
    11. num2 = temp;
    12. cout << "交换后的数据 num1 = " << num1 << endl;
    13. cout << "交换后的数据 num2 = " << num2 << endl;
    14. }
    15. int main() {
    16. int a = 10;
    17. int b = 20;
    18. swap(a, b);
    19. // 当我们做值传递的时候,函数的形参发生变化,不会影响实参
    20. cout << "a = " << a << endl;
    21. cout << "b = " << b << endl;
    22. return 0;
    23. }