<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # C 編程中的按引用函數調用 > 原文: [https://beginnersbook.com/2014/01/c-function-call-by-reference-example/](https://beginnersbook.com/2014/01/c-function-call-by-reference-example/) 在我們討論按引用函數調用之前,讓我們理解我們將在解釋這個時使用的術語: **實際參數**:函數調用中出現的參數。 **形式參數**:函數聲明中出現的參數。 例如:我們有這樣的函數聲明: ```c int sum(int a, int b); ``` `a`和`b`參數是形式參數。 我們正在調用這樣的函數: ```c int s = sum(10, 20); //Here 10 and 20 are actual parameters or int s = sum(n1, n2); //Here n1 and n2 are actual parameters ``` 在本指南中,我們將討論按引用函數調用方法。如果要閱讀按值調用方法,請參考本指南:[按值函數調用](https://beginnersbook.com/2014/01/c-function-call-by-value-example/)。 讓我們回到原點。 **什么是按引用函數調用?** 當我們通過傳遞實際參數的地址來調用函數時,這種調用函數的方式稱為按引用調用。在按引用調用時,對形式參數執行的操作會影響實際參數的值,因為對值執行的所有操作都存儲在實際參數的地址中。它可能聽起來很混亂,但下面的例子將清除你的懷疑。 ## 按引用函數調用的示例 讓我們舉一個簡單的例子。閱讀以下程序中的注釋。 ```c #include <stdio.h> void increment(int *var) { /* Although we are performing the increment on variable * var, however the var is a pointer that holds the address * of variable num, which means the increment is actually done * on the address where value of num is stored. */ *var = *var+1; } int main() { int num=20; /* This way of calling the function is known as call by * reference. Instead of passing the variable num, we are * passing the address of variable num */ increment(&num); printf("Value of num is: %d", num); return 0; } ``` **輸出:** ```c Value of num is: 21 ``` ## 示例 2:按引用函數調用 - 交換數字 這里我們使用按引用調用來交換數字。正如您所看到的,在調用`swapnum()`函數后,變量的值已經更改,因為交換發生在變量`num1`和`num2`的地址上。 ```c #include void swapnum ( int *var1, int *var2 ) { int tempnum ; tempnum = *var1 ; *var1 = *var2 ; *var2 = tempnum ; } int main( ) { int num1 = 35, num2 = 45 ; printf("Before swapping:"); printf("\nnum1 value is %d", num1); printf("\nnum2 value is %d", num2); /*calling swap function*/ swapnum( &num1, &num2 ); printf("\nAfter swapping:"); printf("\nnum1 value is %d", num1); printf("\nnum2 value is %d", num2); return 0; } ``` **輸出:** ```c Before swapping: num1 value is 35 num2 value is 45 After swapping: num1 value is 45 num2 value is 35 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看