- UID
- 390967
- 帖子
- 1578
- 主題
- 814
- 精華
- 0
- 積分
- 852
- 楓幣
- 10316
- 威望
- 393
- 存款
- 10100
- 贊助金額
- 1800
- 推廣
- 0
- GP
- 2507
- 閱讀權限
- 150
- 在線時間
- 187 小時
- 註冊時間
- 2023-5-18
- 最後登入
- 2024-11-14
|
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傳遞參數給函式會實質變更變數數值! |
|