<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/2017/08/cpp-default-arguments/](https://beginnersbook.com/2017/08/cpp-default-arguments/) 在調用函數時不提供任何參數或僅提供少量參數時,將使用默認參數。在編譯程序期間使用默認參數。例如,假設您有一個[用戶定義的函數](https://beginnersbook.com/2017/08/cpp-functions/)`sum`聲明如下:`int sum(int a=10, int b=20)`,現在在調用此函數時,您不提供任何參數,簡稱為`sum()`;那么在這種情況下結果將是 30,編譯器使用函數簽名中聲明的默認值 10 和 20。如果你只傳遞一個這樣的參數:`sum(80)`那么結果將是 100,使用傳遞的參數 80 作為第一個值,20 個從默認參數中獲取。 ## 示例:C++ 中的默認參數 ```cpp #include <iostream> using namespace std; int sum(int a, int b=10, int c=20); int main(){ /* In this case a value is passed as * 1 and b and c values are taken from * default arguments. */ cout<<sum(1)<<endl; /* In this case a value is passed as * 1 and b value as 2, value of c values is * taken from default arguments. */ cout<<sum(1, 2)<<endl; /* In this case all the three values are * passed during function call, hence no * default arguments have been used. */ cout<<sum(1, 2, 3)<<endl; return 0; } int sum(int a, int b, int c){ int z; z = a+b+c; return z; } ``` **輸出:** ```cpp 31 23 6 ``` ## 默認參數的規則 正如您在上面的示例中所看到的,我在函數聲明期間僅為兩個參數`b`和`c`分配了默認值。您可以為所有參數或僅選定的參數指定默認值,但在僅為某些參數指定默認值時,請記住以下規則: > **如果為參數指定默認值,則必須為后續參數分配默認值,否則將出現編譯錯誤。** **例如:**讓我們看一些有效和無效的案例。 **有效:**以下函數聲明有效: ```cpp int sum(int a=10, int b=20, int c=30); int sum(int a, int b=20, int c=30); int sum(int a, int b, int c=30); ``` **無效:**以下函數聲明無效: ```cpp /* Since a has default value assigned, all the * arguments after a (in this case b and c) must have * default values assigned */ int sum(int a=10, int b, int c=30); /* Since b has default value assigned, all the * arguments after b (in this case c) must have * default values assigned */ int sum(int a, int b=20, int c); /* Since a has default value assigned, all the * arguments after a (in this case b and c) must have * default values assigned, b has default value but * c doesn't have, thats why this is also invalid */ int sum(int a=10, int b=20, int c); ```
                  <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>

                              哎呀哎呀视频在线观看