- UID
- 390967
- 帖子
- 1595
- 主題
- 822
- 精華
- 0
- 積分
- 854
- 楓幣
- 11017
- 威望
- 395
- 存款
- 10100
- 贊助金額
- 1800
- 推廣
- 0
- GP
- 2680
- 閱讀權限
- 150
- 在線時間
- 189 小時
- 註冊時間
- 2023-5-18
- 最後登入
- 2024-11-21
|
1. Call by Value- int main()
- {
- int a = 1;
- int b = a;
- return 0;
- }
複製代碼 2. Call by Pointer- void swap(int *address_a, int *address_b) {
- int temp = *address_a ;
- *address_a = *address_b;
- *address_b = temp;
- }
- int main() {
- int a = 1;
- int b = 0;
- swap(&a, &b); // 傳入a, b的address進行交換
- return 0;
- }
複製代碼 3. Call by Reference- void swap(int &address_a, int &address_b) {
- int temp = address_a ;
- address_a = address_b;
- address_b = temp;
- }
- int main() {
- int a = 1;
- int b = 0;
- swap(a, b); // 傳入a, b的address進行交換
- return 0;
- }
複製代碼 使用Call by Pointer跟Call by Reference傳遞參數給函式會實質變更變數數值! |
|