【导读】
本篇文章讲述的是函数调用时,如何使用参数、返回类型。
首先,给出三个经常被举出来的例子:
1 #include <iostream> 2 3 using namespace std; 4 5 void testSwap_val(); 6 void testSwap_ptr(); 7 void testSwap_quote(); 8 9 void swap_val(int a, int b) // 传值调用 10 { 11 int temp = a; 12 a = b; 13 b = a; 14 } 15 16 void swap_ptr(int *a, int *b) // 传地址调用 17 { 18 int temp = *a; 19 *a = *b; 20 *b = temp; 21 } 22 23 void swap_quote(int &a, int &b) // 直接绑定实参 24 { 25 int temp = a; 26 a = b; 27 b = temp; 28 } 29 30 31 int main() 32 { 33 testSwap_val(); 34 testSwap_ptr(); 35 testSwap_quote(); 36 return 0; 37 } 38 39 void testSwap_val() 40 { 41 int m = 2, n = 5; 42 swap_val(m, n); 43 cout << m << " " << n << endl; 44 } 45 46 void testSwap_ptr() 47 { 48 int m = 2, n = 5; 49 swap_ptr(&m, &n); 50 cout << m << " " << n << endl; 51 } 52 53 void testSwap_quote() 54 { 55 int m = 2, n = 5; 56 swap_quote(m, n); 57 cout << m << " " << n << endl; 58 }