{"id":329,"date":"2020-11-02T18:07:15","date_gmt":"2020-11-02T22:07:15","guid":{"rendered":"http:\/\/www.prglab.com\/blog\/?page_id=329"},"modified":"2020-11-02T18:07:15","modified_gmt":"2020-11-02T22:07:15","slug":"functions1","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/cplusplus-tutorial\/program-structure\/functions1","title":{"rendered":"\u51fd\u6570I"},"content":{"rendered":"\n<h2>2.2 \u51fd\u6570I\uff08Functions I\uff09<\/h2>\n\n\n\n<p>\u901a\u8fc7\u4f7f\u7528\u51fd\u6570(functions)\u6211\u4eec\u53ef\u4ee5\u628a\u6211\u4eec\u7684\u7a0b\u5e8f\u4ee5\u66f4\u6a21\u5757\u5316\u7684\u5f62\u5f0f\u7ec4\u7ec7\u8d77\u6765\uff0c\u4ece\u800c\u5229\u7528C++\u6240\u80fd\u63d0\u4f9b\u7684\u6240\u6709\u7ed3\u6784\u5316\u7f16\u7a0b\u7684\u6f5c\u529b\u3002<\/p>\n\n\n\n<p>\u4e00\u4e2a\u51fd\u6570(function)\u662f\u4e00\u4e2a\u53ef\u4ee5\u4ece\u7a0b\u5e8f\u5176\u5b83\u5730\u65b9\u8c03\u7528\u6267\u884c\u7684\u8bed\u53e5\u5757\u3002\u4ee5\u4e0b\u662f\u5b83\u7684\u683c\u5f0f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>type name ( argument1, argument2, ...) statement<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u91cc\uff1a<\/p>\n\n\n\n<ul><li>type \u662f\u51fd\u6570\u8fd4\u56de\u7684\u6570\u636e\u7684\u7c7b\u578b<\/li><li>name \u662f\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u4f7f\u7528\u7684\u540d<\/li><li>argument \u662f\u51fd\u6570\u8c03\u7528\u9700\u8981\u4f20\u5165\u7684\u53c2\u91cf(\u53ef\u4ee5\u58f0\u660e\u4efb\u610f\u591a\u4e2a\u53c2\u91cf)\u3002\u6bcf\u4e2a\u53c2\u91cf(argument)\u7531\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u540e\u9762\u8ddf\u4e00\u4e2a\u6807\u8bc6\u540d\u79f0\u7ec4\u6210\uff0c\u5c31\u50cf\u53d8\u91cf\u58f0\u660e\u4e2d\u4e00\u6837(\u4f8b\u5982\uff0cint x)\u3002\u53c2\u91cf\u4ec5\u5728\u51fd\u6570\u8303\u56f4\u5185\u6709\u6548\uff0c\u53ef\u4ee5\u548c\u51fd\u6570\u4e2d\u7684\u5176\u5b83\u53d8\u91cf\u4e00\u6837\u4f7f\u7528\uff0c \u5b83\u4eec\u4f7f\u5f97\u51fd\u6570\u5728\u88ab\u8c03\u7528\u65f6\u53ef\u4ee5\u4f20\u5165\u53c2\u6570\uff0c\u4e0d\u540c\u7684\u53c2\u6570\u7528\u9017\u53f7(comma)\u9694\u5f00.<\/li><li>statement \u662f\u51fd\u6570\u7684\u5185\u5bb9\u3002\u5b83\u53ef\u4ee5\u662f\u4e00\u53e5\u6307\u4ee4\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u7ec4\u6307\u4ee4\u7ec4\u6210\u7684\u8bed\u53e5\u5757\u3002\u5982\u679c\u662f\u4e00\u7ec4\u6307\u4ee4\uff0c\u5219\u8bed\u53e5\u5757\u5fc5\u987b\u7528\u82b1\u62ec\u53f7{}\u62ec\u8d77\u6765\uff0c\u8fd9\u4e5f\u662f\u6211\u4eec\u6700\u5e38\u89c1\u5230\u60c5\u51b5\u3002\u5176\u5b9e\u4e3a\u4e86\u4f7f\u7a0b\u5e8f\u7684\u683c\u5f0f\u66f4\u52a0\u7edf\u4e00\u6e05\u6670\uff0c\u5efa\u8bae\u5728\u4ec5\u6709\u4e00\u6761\u6307\u4ee4\u7684\u65f6\u5019\u4e5f\u4f7f\u7528\u82b1\u62ec\u53f7\uff0c\u8fd9\u662f\u4e00\u4e2a\u826f\u597d\u7684\u7f16\u7a0b\u4e60\u60ef\u3002<\/li><\/ul>\n\n\n\n<p>\u4e0b\u9762\u770b\u4e00\u4e0b\u7b2c\u4e00\u4e2a\u51fd\u6570\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ function example<br>#include &lt;iostream.h&gt;<br>int addition (int a, int b)<br>{<br>&nbsp;&nbsp;&nbsp; int r;<br>&nbsp;&nbsp;&nbsp; r=a+b;<br>&nbsp;&nbsp;&nbsp; return (r);<br>}<br><br>int main ()<br>{<br>&nbsp;&nbsp;&nbsp; int z;<br>&nbsp;&nbsp;&nbsp; z = addition (5,3);<br>&nbsp;&nbsp;&nbsp; cout &lt;&lt; &#8220;The result is &#8221; &lt;&lt; z;<br>&nbsp;&nbsp;&nbsp; return 0;<br>}<\/td><td>The result is 8<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8bb0\u5f97\u5728\u6211\u4eec\u6559\u7a0b\u5f00\u59cb\u65f6\u8bf4\u8fc7\uff1a\u4e00\u4e2aC++\u7a0b\u5e8f\u603b\u662f\u4ecemain\u51fd\u6570\u5f00\u59cb\u6267\u884c\u3002 \u56e0\u6b64\u6211\u4eec\u4ece\u90a3\u91cc\u5f00\u59cb\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230 main \u51fd\u6570\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6574\u578b\u53d8\u91cfz \u5f00\u59cb\u3002\u7d27\u8ddf\u7740\u6211\u4eec\u770b\u5230\u8c03\u7528addition \u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u51fd\u6570\u8c03\u7528\u7684\u5199\u6cd5\u548c\u4e0a\u9762\u51fd\u6570\u5b9a\u4e49\u672c\u8eab\u5341\u5206\u76f8\u4f3c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.prglab.com\/cms\/media\/images\/cplusplus\/img2-2-1.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u53c2\u6570\u6709\u660e\u663e\u7684\u5bf9\u5e94\u5173\u7cfb\u3002\u5728main \u51fd\u6570\u4e2d\u6211\u4eec\u8c03\u7528addition \u51fd\u6570\uff0c\u5e76\u4f20\u5165\u4e24\u4e2a\u6570\u503c\uff1a 5 \u548c3 \uff0c\u3000\u5b83\u4eec\u5bf9\u5e94\u51fd\u6570addition \u4e2d\u5b9a\u4e49\u7684\u53c2\u6570int a \u548cint b\u3002<\/p>\n\n\n\n<p>\u5f53\u51fd\u6570\u5728main \u4e2d\u88ab\u8c03\u7528\u65f6\uff0c\u7a0b\u5e8f\u6267\u884c\u7684\u63a7\u5236\u6743\u4ecemain\u8f6c\u79fb\u5230\u51fd\u6570addition\u3002\u8c03\u7528\u4f20\u9012\u7684\u4e24\u4e2a\u53c2\u6570\u7684\u6570\u503c (5 \u548c3) \u88ab\u590d\u5236\u5230\u51fd\u6570\u7684\u672c\u5730\u53d8\u91cf\uff08local variables\uff09 int a \u548cint b \u4e2d\u3002<\/p>\n\n\n\n<p>\u51fd\u6570addition \u4e2d\u5b9a\u4e49\u4e86\u65b0\u7684\u53d8\u91cf(int r;)\uff0c\u901a\u8fc7\u8868\u8fbe\u5f0fr=a+b;, \u5b83\u628aa \u52a0b \u7684\u7ed3\u679c\u8d4b\u7ed9r \u3002\u56e0\u4e3a\u4f20\u8fc7\u6765\u7684\u53c2\u6570a \u548cb \u7684\u503c\u5206\u522b\u4e3a5 \u548c3 \uff0c\u6240\u4ee5\u7ed3\u679c\u662f8\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u4e00\u884c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p><code>return (r);<\/code><\/p>\n\n\n\n<p>\u7ed3\u675f\u51fd\u6570addition\uff0c\u5e76\u628a\u63a7\u5236\u6743\u4ea4\u8fd8\u7ed9\u8c03\u7528\u5b83\u7684\u51fd\u6570(main) \uff0c\u4ece\u8c03\u7528addition\u7684\u5730\u65b9\u5f00\u59cb\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\u3002\u53e6\u5916\uff0creturn \u5728\u8c03\u7528\u7684\u65f6\u5019\u540e\u9762\u8ddf\u7740\u53d8\u91cfr (return (r);), \u5b83\u5f53\u65f6\u7684\u503c\u4e3a8, \u8fd9\u4e2a\u503c\u88ab\u79f0\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.prglab.com\/cms\/media\/images\/cplusplus\/img2-2-2.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u51fd\u6570\u8fd4\u56de\u7684\u6570\u503c\u5c31\u662f\u51fd\u6570\u7684\u8ba1\u7b97\u7ed3\u679c\uff0c\u56e0\u6b64\uff0c z \u5c06\u5b58\u50a8\u51fd\u6570addition (5, 3)\u8fd4\u56de\u7684\u6570\u503c, \u53738\u3002\u7528\u53e6\u4e00\u79cd\u65b9\u5f0f\u89e3\u91ca\uff0c\u4f60\u4e5f\u53ef\u4ee5\u60f3\u8c61\u6210\u8c03\u7528\u51fd\u6570(addition (5,3)) \u88ab\u66ff\u6362\u6210\u4e86\u5b83\u7684\u8fd4\u56de\u503c (8)\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765main\u4e2d\u7684\u4e0b\u4e00\u884c\u4ee3\u7801\u662f\uff1a<\/p>\n\n\n\n<p><code>cout &lt;&lt; \"The result is \" &lt;&lt; z;<\/code><\/p>\n\n\n\n<p>\u5b83\u628a\u7ed3\u679c\u6253\u5370\u5728\u5c4f\u5e55\u4e0a\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><img src=\"http:\/\/www.prglab.com\/cms\/media\/images\/cplusplus\/img2-2-3.gif\"><strong>\u53d8\u91cf\u7684\u8303\u56f4(Scope of variables)<\/strong>\u4f60\u5fc5\u987b\u8003\u8651\u5230\u53d8\u91cf\u7684\u8303\u56f4\u53ea\u662f\u5728\u5b9a\u4e49\u8be5\u53d8\u91cf\u7684\u51fd\u6570\u6216\u6307\u4ee4\u5757\u5185\u6709\u6548\uff0c\u800c\u4e0d\u80fd\u5728\u5b83\u7684\u51fd\u6570\u6216\u6307\u4ee4\u5757\u4e4b\u5916\u4f7f\u7528\u3002 \u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u91cc\u5c31\u4e0d\u53ef\u80fd\u5728main \u4e2d\u76f4\u63a5\u4f7f\u7528\u53d8\u91cfa, b \u6216 r \uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u51fd\u6570addition\u7684\u672c\u5730\u53d8\u91cf\uff08local variable\uff09\u3002\u5728\u51fd\u6570addition\u4e2d\u4e5f\u4e0d\u53ef\u80fd\u76f4\u63a5\u4f7f\u7528\u53d8\u91cfz\uff0c\u56e0\u4e3a\u5b83\u662fmain\u7684\u672c\u5730\u53d8\u91cf\u3002\u56e0\u6b64\uff0c\u672c\u5730\u53d8\u91cf (local variables)\u7684\u8303\u56f4\u662f\u5c40\u9650\u4e8e\u58f0\u660e\u5b83\u7684\u5d4c\u5957\u8303\u56f4\u4e4b\u5185\u7684\u3002\u5c3d\u7ba1\u5982\u6b64\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf(global variables)\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u4efb\u4f55\u4f4d\u7f6e\u88ab\u8bbf\u95ee\uff0c\u4e0d\u7ba1\u5728\u51fd\u6570\u4ee5\u5185\u8fd8\u662f\u4ee5\u5916\u3002\u8981\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\uff0c\u4f60\u5fc5\u987b\u5728\u6240\u6709\u51fd\u6570\u6216\u4ee3\u7801\u5757\u4e4b\u5916\u5b9a\u4e49\u5b83\u4eec\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u76f4\u63a5\u5728\u7a0b\u5e8f\u4f53\u4e2d\u58f0\u660e\u5b83\u4eec\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u662f\u53e6\u4e00\u4e2a\u5173\u4e8e\u51fd\u6570\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ function example<br>#include &lt;iostream.h&gt;<br>int subtraction (int a, int b)<br>{<br>&nbsp;&nbsp;&nbsp; int r;<br>&nbsp;&nbsp;&nbsp; r=a-b;<br>&nbsp;&nbsp;&nbsp; return (r);<br>}<br><br>int main ()<br>{<br>&nbsp;&nbsp;&nbsp; int x=5, y=3, z;<br>&nbsp;&nbsp;&nbsp; z = subtraction (7,2);<br>&nbsp;&nbsp;&nbsp; cout &lt;&lt; &#8220;The first result is &#8221; &lt;&lt; z &lt;&lt; &#8216;\\n&#8217;;<br>&nbsp;&nbsp;&nbsp; cout &lt;&lt; &#8220;The second result is &#8221; &lt;&lt; subtraction (7,2) &lt;&lt; &#8216;\\n&#8217;;<br>&nbsp;&nbsp;&nbsp; cout &lt;&lt; &#8220;The third result is &#8221; &lt;&lt; subtraction (x,y) &lt;&lt; &#8216;\\n&#8217;;<br>&nbsp;&nbsp;&nbsp; z= 4 + subtraction (x,y);<br>&nbsp;&nbsp;&nbsp; cout &lt;&lt; &#8220;The fourth result is &#8221; &lt;&lt; z &lt;&lt; &#8216;\\n&#8217;;<br>&nbsp;&nbsp;&nbsp; return 0;<br>}<\/td><td>The first result is 5<br>The second result is 5<br>The third result is 2<br>The fourth result is 6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u51fd\u6570subtraction\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u529f\u80fd\u662f\u8ba1\u7b97\u4f20\u5165\u7684\u4e24\u4e2a\u53c2\u6570\u7684\u5dee\u503c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u3002<\/p>\n\n\n\n<p>\u5728 main \u51fd\u6570\u4e2d\uff0c\u51fd\u6570subtraction\u88ab\u8c03\u7528\u4e86\u591a\u6b21\u3002\u6211\u4eec\u7528\u4e86\u51e0\u79cd\u4e0d\u540c\u7684\u8c03\u7528\u65b9\u6cd5\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u770b\u5230\u5728\u4e0d\u540c\u7684\u60c5\u51b5\u4e0b\u51fd\u6570\u5982\u4f55\u88ab\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u66f4\u597d\u7684\u7406\u89e3\u8fd9\u4e9b\u4f8b\u5b50\uff0c\u4f60\u9700\u8981\u8003\u8651\u5230\u88ab\u8c03\u7528\u7684\u51fd\u6570\u5176\u5b9e\u5b8c\u5168\u53ef\u4ee5\u7531\u5b83\u6240\u8fd4\u56de\u7684\u503c\u6765\u4ee3\u66ff\u3002\u4f8b\u5982\u5728\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7b2c\u4e00\u79cd\u60c5\u51b5\u4e0b (\u8fd9\u79cd\u8c03\u7528\u4f60\u5e94\u8be5\u5df2\u7ecf\u77e5\u9053\u4e86\uff0c\u56e0\u4e3a\u6211\u4eec\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\u5df2\u7ecf\u7528\u8fc7\u8fd9\u79cd\u5f62\u5f0f\u7684\u8c03\u7528):z = subtraction (7,2);<br>cout &lt;&lt; &#8220;The first result is &#8221; &lt;&lt; z;<\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u628a\u51fd\u6570\u8c03\u7528\u7528\u5b83\u7684\u7ed3\u679c(\u4e5f\u5c31\u662f5)\u66ff\u6362\uff0c\u6211\u4eec\u5c06\u5f97\u5230\uff1az = 5;<br>cout &lt;&lt; &#8220;The first result is &#8221; &lt;&lt; z;<\/p>\n\n\n\n<p>\u540c\u6837\u7684<\/p>\n\n\n\n<p><code>cout &lt;&lt; \"The second result is \" &lt;&lt; subtraction (7,2);<\/code><\/p>\n\n\n\n<p>\u4e0e\u524d\u9762\u7684\u8c03\u7528\u6709\u540c\u6837\u7684\u7ed3\u679c\uff0c\u4f46\u5728\u8fd9\u91cc\u6211\u4eec\u628a\u5bf9\u51fd\u6570subtraction \u7684\u8c03\u7528\u76f4\u63a5\u7528\u4f5ccout\u7684\u53c2\u6570\u3002\u8fd9\u53ef\u4ee5\u7b80\u5355\u60f3\u8c61\u6210\u6211\u4eec\u5199\u7684\u662f\uff1a<\/p>\n\n\n\n<p><code>cout &lt;&lt; \"The second result is \" &lt;&lt; 5;<\/code><\/p>\n\n\n\n<p>\u56e0\u4e3a5 \u662fsubtraction (7,2)\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u5728<\/p>\n\n\n\n<p><code>cout &lt;&lt; \"The third result is \" &lt;&lt; subtraction (x,y);<\/code><\/p>\n\n\n\n<p>\u4e2d\uff0c\u4e0e\u524d\u9762\u7684\u8c03\u7528\u552f\u4e00\u7684\u4e0d\u540c\u4e4b\u5904\u662f\u8fd9\u91cc\u8c03\u7528subtraction \u65f6\u7684\u53c2\u6570\u4f7f\u7528\u7684\u662f\u53d8\u91cf\u800c\u4e0d\u662f\u5e38\u91cf\u3002\u8fd9\u6837\u7528\u65f6\u6beb\u65e0\u95ee\u9898\u7684\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\uff0c\u4f20\u5165\u51fd\u6570subtraction \u7684\u53c2\u6570\u503c\u662f\u53d8\u91cfx \u548cy\u4e2d\u5b58\u50a8\u7684\u6570\u503c\uff0c\u5373\u5206\u522b\u4e3a5 \u548c3\uff0c\u7ed3\u679c\u4e3a2\u3002<\/p>\n\n\n\n<p>\u7b2c\u56db\u79cd\u8c03\u7528\u4e5f\u662f\u4e00\u6837\u7684\u3002\u53ea\u8981\u77e5\u9053\u9664\u4e86<\/p>\n\n\n\n<p><code>z = 4 + subtraction (x,y);<\/code><\/p>\n\n\n\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u5199\u6210\uff1a<\/p>\n\n\n\n<p>z = subtraction (x,y) + 4;<\/p>\n\n\n\n<p>\u5b83\u4eec\u7684\u7ed3\u679c\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002\u6ce8\u610f\u5728\u6574\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u5c3e\u5199\u4e0a\u5206\u53f7semicolon sign (;)\u3002\u5b83\u5e76\u4e0d\u9700\u8981\u603b\u662f\u8ddf\u5728\u51fd\u6570\u8c03\u7528\u7684\u540e\u9762\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u6709\u4e00\u6b21\u628a\u5b83\u4eec\u60f3\u8c61\u6210\u51fd\u6570\u88ab\u5b83\u7684\u7ed3\u679c\u6240\u66ff\u4ee3\uff1a<\/p>\n\n\n\n<p><code>z = 4 + 2;<br>z = 2 + 4;<\/code><\/p>\n\n\n\n<h3>\u6ca1\u6709\u8fd4\u56de\u503c\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u4f7f\u7528void.<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u8bb0\u5f97\u51fd\u6570\u58f0\u660e\u7684\u683c\u5f0f\uff1a<\/p>\n\n\n\n<p><code>type name ( argument1, argument2 ...) statement<\/code><\/p>\n\n\n\n<p>\u5c31\u4f1a\u77e5\u9053\u51fd\u6570\u58f0\u660e\u5fc5\u987b\u4ee5\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\uff08type\uff09\u5f00\u5934\uff0c\u5b83\u662f\u51fd\u6570\u7531return \u8bed\u53e5\u6240\u8fd4\u56de\u6570\u636e\u7c7b\u578b\u3002\u4f46\u662f\u5982\u679c\u6211\u4eec\u5e76\u4e0d\u6253\u7b97\u8fd4\u56de\u4efb\u4f55\u6570\u636e\u90a3\u8be5\u600e\u4e48\u529e\u5462\uff1f<\/p>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u8981\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u7684\u529f\u80fd\u662f\u6253\u5370\u5728\u5c4f\u5e55\u4e0a\u6253\u5370\u4e00\u4e9b\u4fe1\u606f\u3002\u6211\u4eec\u4e0d\u9700\u8981\u5b83\u8fd4\u56de\u4efb\u4f55\u503c\uff0c\u800c\u4e14\u6211\u4eec\u4e5f\u4e0d\u9700\u8981\u5b83\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\u3002C\u8bed\u8a00\u4e3a\u8fd9\u4e9b\u60c5\u51b5\u8bbe\u8ba1\u4e86void \u7c7b\u578b\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ void \u51fd\u6570\u793a\u4f8b<br>#include &lt;iostream&gt;<br>using namespace std;<br><br>void printmessage ()<br>{<br>cout &lt;&lt; &#8220;I&#8217;m a function!&#8221;;<br>}<br><br>int main ()<br>{<br>printmessage ();<br>return 0;<br>}<\/td><td>I&#8217;m a function!<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>void\u8fd8\u53ef\u4ee5\u88ab\u7528\u5728\u51fd\u6570\u53c2\u6570\u4f4d\u7f6e\uff0c\u8868\u793a\u6211\u4eec\u660e\u786e\u5e0c\u671b\u8fd9\u4e2a\u51fd\u6570\u5728\u88ab\u8c03\u7528\u65f6<strong>\u4e0d<\/strong>\u9700\u8981\u4efb\u4f55\u53c2\u6570\u3002\u4f8b\u5982\u4e0a\u9762\u7684\u51fd\u6570printmessage\u4e5f\u53ef\u4ee5\u5199\u4e3a\u4ee5\u4e0b\u5f62\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void printmessage (void)<br>{<br>  cout &lt;&lt; \"I'm a function!\";<br>}<\/pre>\n\n\n\n<p>\u867d\u7136\u5728C++ \u4e2dvoid\u53ef\u4ee5\u88ab\u7701\u7565\uff0c\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u5199\u51favoid\uff0c\u4ee5\u4fbf\u660e\u786e\u6307\u51fa\u51fd\u6570\u4e0d\u9700\u8981\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u4f60\u5fc5\u987b\u65f6\u523b\u77e5\u9053\u7684\u662f\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u65f6\u8981\u5199\u51fa\u5b83\u7684\u540d\u5b57\u5e76\u628a\u53c2\u6570\u5199\u5728\u540e\u9762\u7684\u62ec\u53f7\u5185\u3002\u4f46\u5982\u679c\u51fd\u6570\u4e0d\u9700\u8981\u53c2\u6570\uff0c\u540e\u9762\u7684\u62ec\u53f7\u5e76\u4e0d\u80fd\u7701\u7565\u3002\u56e0\u6b64\u8c03\u7528\u51fd\u6570 printmessage \u7684\u683c\u5f0f\u662fprintmessage();<\/p>\n\n\n\n<p>\u51fd\u6570\u540d\u79f0\u540e\u9762\u7684\u62ec\u53f7\u5c31\u660e\u786e\u8868\u793a\u4e86\u5b83\u662f\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u53d8\u91cf\u540d\u79f0\u6216\u5176\u5b83\u4ec0\u4e48\u8bed\u53e5\u3002\u4ee5\u4e0b\u8c03\u7528\u51fd\u6570\u7684\u65b9\u5f0f\u5c31\u4e0d\u5bf9\uff1aprintmessage;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>2.2 \u51fd\u6570I\uff08Functions I\uff09 \u901a\u8fc7\u4f7f\u7528\u51fd\u6570(functions)\u6211\u4eec\u53ef\u4ee5\u628a\u6211\u4eec\u7684\u7a0b\u5e8f\u4ee5\u66f4\u6a21\u5757\u5316\u7684\u5f62\u5f0f\u7ec4\u7ec7\u8d77\u6765\uff0c\u4ece\u800c\u5229\u7528C++\u6240\u80fd\u63d0\u4f9b\u7684\u6240\u6709\u7ed3\u6784\u5316\u7f16\u7a0b\u7684\u6f5c\u529b\u3002 \u4e00\u4e2a\u51fd\u6570(function)\u662f\u4e00\u4e2a\u53ef\u4ee5\u4ece\u7a0b\u5e8f\u5176\u5b83\u5730\u65b9\u8c03\u7528\u6267\u884c\u7684\u8bed\u53e5\u5757\u3002\u4ee5\u4e0b\u662f\u5b83\u7684\u683c\u5f0f\uff1a type name ( argument1, argument2, &#8230;) statement \u8fd9\u91cc\uff1a type \u662f\u51fd\u6570\u8fd4\u56de\u7684\u6570\u636e\u7684\u7c7b\u578b name \u662f\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u4f7f\u7528\u7684\u540d argument \u662f\u51fd\u6570\u8c03\u7528\u9700\u8981\u4f20\u5165\u7684\u53c2\u91cf(\u53ef\u4ee5\u58f0\u660e\u4efb\u610f\u591a\u4e2a\u53c2\u91cf)\u3002\u6bcf\u4e2a\u53c2\u91cf(argument)\u7531\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u540e\u9762\u8ddf\u4e00\u4e2a\u6807\u8bc6\u540d\u79f0\u7ec4\u6210\uff0c\u5c31\u50cf\u53d8\u91cf\u58f0\u660e\u4e2d\u4e00\u6837(\u4f8b\u5982\uff0cint x)\u3002\u53c2\u91cf\u4ec5\u5728\u51fd\u6570\u8303\u56f4\u5185\u6709\u6548\uff0c\u53ef\u4ee5\u548c\u51fd\u6570\u4e2d\u7684\u5176\u5b83\u53d8\u91cf\u4e00\u6837\u4f7f\u7528\uff0c \u5b83\u4eec\u4f7f\u5f97\u51fd\u6570\u5728\u88ab\u8c03\u7528\u65f6\u53ef\u4ee5\u4f20\u5165\u53c2\u6570\uff0c\u4e0d\u540c\u7684\u53c2\u6570\u7528\u9017\u53f7(comma)\u9694\u5f00. statement \u662f\u51fd\u6570\u7684\u5185\u5bb9\u3002\u5b83\u53ef\u4ee5\u662f\u4e00\u53e5\u6307\u4ee4\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u7ec4\u6307\u4ee4\u7ec4\u6210\u7684\u8bed\u53e5\u5757\u3002\u5982\u679c\u662f\u4e00\u7ec4\u6307\u4ee4\uff0c\u5219\u8bed\u53e5\u5757\u5fc5\u987b\u7528\u82b1\u62ec\u53f7{}\u62ec\u8d77\u6765\uff0c\u8fd9\u4e5f\u662f\u6211\u4eec\u6700\u5e38\u89c1\u5230\u60c5\u51b5\u3002\u5176\u5b9e\u4e3a\u4e86\u4f7f\u7a0b\u5e8f\u7684\u683c\u5f0f\u66f4\u52a0\u7edf\u4e00\u6e05\u6670\uff0c\u5efa\u8bae\u5728\u4ec5\u6709\u4e00\u6761\u6307\u4ee4\u7684\u65f6\u5019\u4e5f\u4f7f\u7528\u82b1\u62ec\u53f7\uff0c\u8fd9\u662f\u4e00\u4e2a\u826f\u597d\u7684\u7f16\u7a0b\u4e60\u60ef\u3002 \u4e0b\u9762\u770b\u4e00\u4e0b\u7b2c\u4e00\u4e2a\u51fd\u6570\u7684\u4f8b\u5b50\uff1a \/\/ function example#include &lt;iostream.h&gt;int addition (int a, int b){&nbsp;&nbsp;&nbsp; int r;&nbsp;&nbsp;&nbsp; r=a+b;&nbsp;&nbsp;&nbsp; return (r);} int main (){&nbsp;&nbsp;&nbsp; int z;&nbsp;&nbsp;&nbsp; z = addition (5,3);&nbsp;&nbsp;&nbsp; cout &lt;&lt; &#8220;The result is &#8221; &lt;&lt; z;&nbsp;&nbsp;&nbsp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":325,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/329"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=329"}],"version-history":[{"count":1,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/329\/revisions"}],"predecessor-version":[{"id":330,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/329\/revisions\/330"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/325"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}