{"id":331,"date":"2020-11-02T18:08:57","date_gmt":"2020-11-02T22:08:57","guid":{"rendered":"http:\/\/www.prglab.com\/blog\/?page_id=331"},"modified":"2020-11-02T18:08:57","modified_gmt":"2020-11-02T22:08:57","slug":"functions2","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/cplusplus-tutorial\/program-structure\/functions2","title":{"rendered":"\u51fd\u6570II"},"content":{"rendered":"\n<h2>2.3 \u51fd\u6570II\uff08Functions II\uff09<\/h2>\n\n\n\n<h3>\u53c2\u6570\u6309\u6570\u503c\u4f20\u9012\u548c\u6309\u5730\u5740\u4f20\u9012(Arguments passed by value and by reference)<\/h3>\n\n\n\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u770b\u5230\u7684\u6240\u6709\u51fd\u6570\u4e2d\uff0c\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u7684\u53c2\u6570\u5168\u90e8\u662f\u6309\u6570\u503c\u4f20\u9012\u7684(by value)\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u6211\u4eec\u8c03\u7528\u4e00\u4e2a\u5e26\u6709\u53c2\u6570\u7684\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u7684\u662f\u53d8\u91cf\u7684\u6570\u503c\u800c\u4e0d\u662f\u53d8\u91cf\u672c\u8eab\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u8c03\u7528\u6211\u4eec\u7684\u7b2c\u4e00\u4e2a\u51fd\u6570addition \uff1aint x=5, y=3, z;<br>z = addition ( x , y );<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\u6211\u4eec\u8c03\u7528\u51fd\u6570addition \u540c\u65f6\u5c06<strong>x<\/strong>\u548c<strong>y<\/strong>\u7684\u503c\u4f20\u7ed9\u5b83\uff0c\u5373\u5206\u522b\u4e3a<strong>5<\/strong>\u548c<strong>3<\/strong>\uff0c\u800c\u4e0d\u662f\u4e24\u4e2a\u53d8\u91cf\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.prglab.com\/cms\/media\/images\/cplusplus\/img2-3-1.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u8fd9\u6837\uff0c\u5f53\u51fd\u6570<strong>addition<\/strong>\u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u7684\u53d8\u91cf<strong>a<\/strong>\u548c<strong>b<\/strong>\u7684\u503c\u5206\u522b\u53d8\u4e3a<strong>5<\/strong>\u548c<strong>3<\/strong>\uff0c\u4f46\u5728\u51fd\u6570addition\u5185\u5bf9\u53d8\u91cfa \u6216b \u6240\u505a\u7684\u4efb\u4f55\u4fee\u6539\u4e0d\u4f1a\u5f71\u54cd\u53d8\u91cf\u4ed6\u5916\u9762\u7684\u53d8\u91cfx \u548c y \u7684\u503c\uff0c\u56e0\u4e3a\u53d8\u91cf<strong>x<\/strong>\u548c<strong>y<\/strong>\u5e76\u6ca1\u6709\u628a\u5b83\u4eec\u81ea\u5df1\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u800c\u53ea\u662f\u4f20\u9012\u4e86\u4ed6\u4eec\u7684\u6570\u503c\u3002<\/p>\n\n\n\n<p>\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f60\u53ef\u80fd\u9700\u8981\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u63a7\u5236\u4e00\u4e2a\u51fd\u6570\u4ee5\u5916\u7684\u53d8\u91cf\u3002\u8981\u5b9e\u73b0\u8fd9\u79cd\u64cd\u4f5c\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u6309\u5730\u5740\u4f20\u9012\u7684\u53c2\u6570\uff08arguments passed by reference\uff09\uff0c\u5c31\u8c61\u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u51fd\u6570<strong>duplicate<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ passing parameters by reference<br>#include &lt;iostream.h&gt;<br><br>void duplicate (int&amp; a, int&amp; b, int&amp; c)<br>{<br>a*=2;<br>b*=2;<br>c*=2;<br>}<br><br>int main ()<br>{<br>int x=1, y=3, z=7;<br>duplicate (x, y, z);<br>cout &lt;&lt; &#8220;x=&#8221; &lt;&lt; x &lt;&lt; &#8220;, y=&#8221; &lt;&lt; y &lt;&lt; &#8220;, z=&#8221; &lt;&lt; z;<br>return 0;<br>}<\/td><td>x=2, y=6, z=14<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u5e94\u8be5\u6ce8\u610f\u7684\u4e8b\u9879\u662f\u5728\u51fd\u6570<strong>duplicate<\/strong>\u7684\u58f0\u660e(declaration)\u4e2d\uff0c\u6bcf\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u540e\u9762\u8ddf\u4e86\u4e00\u4e2a\u5730\u5740\u7b26ampersand sign (&amp;)\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u6307\u660e\u53d8\u91cf\u662f\u6309\u5730\u5740\u4f20\u9012\u7684\uff08by reference\uff09\uff0c\u800c\u4e0d\u662f\u50cf\u901a\u5e38\u4e00\u6837\u6309\u6570\u503c\u4f20\u9012\u7684\uff08by value\uff09\u3002<\/p>\n\n\n\n<p>\u5f53\u6309\u5730\u5740\u4f20\u9012\uff08pass by reference\uff09\u4e00\u4e2a\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u6211\u4eec\u662f\u5728\u4f20\u9012\u8fd9\u4e2a\u53d8\u91cf\u672c\u8eab\uff0c\u6211\u4eec\u5728\u51fd\u6570\u4e2d\u5bf9\u53d8\u91cf\u6240\u505a\u7684\u4efb\u4f55\u4fee\u6539\u5c06\u4f1a\u5f71\u54cd\u5230\u51fd\u6570\u5916\u9762\u88ab\u4f20\u9012\u7684\u53d8\u91cf\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.prglab.com\/cms\/media\/images\/cplusplus\/img2-3-2.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u7528\u53e6\u4e00\u79cd\u65b9\u5f0f\u6765\u8bf4\uff0c\u6211\u4eec\u5df2\u7ecf\u628a\u53d8\u91cf<strong>a, b\uff0cc<\/strong>\u548c\u8c03\u7528\u51fd\u6570\u65f6\u4f7f\u7528\u7684\u53c2\u6570(<strong>x, y<\/strong>\u548c&nbsp;<strong>z<\/strong>)\u8054\u7cfb\u8d77\u6765\u4e86\uff0c\u56e0\u6b64\u5982\u679c\u6211\u4eec\u5728\u51fd\u6570\u5185\u5bf9a \u8fdb\u884c\u64cd\u4f5c\uff0c\u51fd\u6570\u5916\u9762\u7684x \u503c\u4e5f\u4f1a\u6539\u53d8\u3002\u540c\u6837\uff0c\u4efb\u4f55\u5bf9b \u7684\u6539\u53d8\u4e5f\u4f1a\u5f71\u54cdy\uff0c\u5bf9c \u7684\u6539\u53d8\u4e5f\u4f1a\u5f71\u54cd<strong>z&gt;<\/strong>\u3002<\/p>\n\n\n\n<p>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4e3b\u7a0b\u5e8f<strong>main<\/strong>\u4e2d\u7684\u4e09\u4e2a\u53d8\u91cf<strong>x, y<\/strong>\u548c<strong>z<\/strong>\u5728\u8c03\u7528\u51fd\u6570duplicate \u540e\u6253\u5370\u7ed3\u679c\u663e\u793a\u4ed6\u4eec\u7684\u503c\u589e\u52a0\u4e86\u4e00\u500d\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u5728\u58f0\u660e\u4e0b\u9762\u7684\u51fd\u6570\uff1a<code>void duplicate (int&amp; a, int&amp; b, int&amp; c)<\/code><\/p>\n\n\n\n<p>\u65f6\uff0c\u6211\u4eec\u662f\u6309\u8fd9\u6837\u58f0\u660e\u7684\uff1a<code>void duplicate (int a, int b, int c)<\/code><\/p>\n\n\n\n<p>\u4e5f\u5c31\u662f\u4e0d\u5199\u5730\u5740\u7b26<strong>&nbsp;ampersand (&amp;)<\/strong>\uff0c\u6211\u4eec\u4e5f\u5c31\u6ca1\u6709\u5c06\u53c2\u6570\u7684\u5730\u5740\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u800c\u662f\u4f20\u9012\u4e86\u5b83\u4eec\u7684\u503c\uff0c\u56e0\u6b64\uff0c\u5c4f\u5e55\u4e0a\u663e\u793a\u7684\u8f93\u51fa\u7ed3\u679c<strong>x, y \uff0cz<\/strong>&nbsp;\u7684\u503c\u5c06\u4e0d\u4f1a\u6539\u53d8\uff0c\u4ecd\u662f<strong>1\uff0c3\uff0c7<\/strong>\u3002<img src=\"http:\/\/www.prglab.com\/cms\/media\/images\/cplusplus\/c-diff.png\"><\/p>\n\n\n\n<p>\u8fd9\u79cd\u7528\u5730\u5740\u7b26 ampersand (&amp;)\u6765\u58f0\u660e\u6309\u5730\u5740&#8221;by reference&#8221;\u4f20\u9012\u53c2\u6570\u7684\u65b9\u5f0f\u53ea\u662f\u5728C++\u4e2d\u9002\u7528\u3002\u5728C \u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u5fc5\u987b\u7528\u6307\u9488(pointers)\u6765\u505a\u76f8\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u6309\u5730\u5740\u4f20\u9012(Passing by reference)\u662f\u4e00\u4e2a\u4f7f\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\u7684\u6709\u6548\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8fd4\u56de\u7b2c\u4e00\u4e2a\u8f93\u5165\u53c2\u6570\u7684\u524d\u4e00\u4e2a\u548c\u540e\u4e00\u4e2a\u6570\u503c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ more than one returning value<br>#include &lt;iostream.h&gt;<br>void prevnext (int x, int&amp; prev, int&amp; next)<br>{<br>prev = x-1;<br>next = x+1;<br>}<br><br>int main ()<br>{<br>int x=100, y, z;<br>prevnext (x, y, z);<br>cout &lt;&lt; &#8220;Previous=&#8221; &lt;&lt; y &lt;&lt; &#8220;, Next=&#8221; &lt;&lt; z;<br>return 0;<br>}<\/td><td>Previous=99, Next=101<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3>\u53c2\u6570\u7684\u9ed8\u8ba4\u503c(Default values in arguments)<\/h3>\n\n\n\n<p>\u5f53\u58f0\u660e\u4e00\u4e2a\u51fd\u6570\u7684\u65f6\u5019\u6211\u4eec\u53ef\u4ee5\u7ed9\u6bcf\u4e00\u4e2a\u53c2\u6570\u6307\u5b9a\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u5982\u679c\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u6ca1\u6709\u7ed9\u51fa\u8be5\u53c2\u6570\u7684\u503c\uff0c\u90a3\u4e48\u8fd9\u4e2a\u9ed8\u8ba4\u503c\u5c06\u88ab\u4f7f\u7528\u3002\u6307\u5b9a\u53c2\u6570\u9ed8\u8ba4\u503c\u53ea\u9700\u8981\u5728\u51fd\u6570\u58f0\u660e\u65f6\u628a\u4e00\u4e2a\u6570\u503c\u8d4b\u7ed9\u53c2\u6570\u3002\u5982\u679c\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u6ca1\u6709\u6570\u503c\u4f20\u9012\u7ed9\u8be5\u53c2\u6570\uff0c\u90a3\u4e48\u9ed8\u8ba4\u503c\u5c06\u88ab\u4f7f\u7528\u3002\u4f46\u5982\u679c\u6709\u6307\u5b9a\u7684\u6570\u503c\u4f20\u9012\u7ed9\u53c2\u6570\uff0c\u90a3\u4e48\u9ed8\u8ba4\u503c\u5c06\u88ab\u6307\u5b9a\u7684\u6570\u503c\u53d6\u4ee3\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ default values in functions<br>#include &lt;iostream.h&gt;<br>int divide (int a, int b=2) {<br>int r;<br>r=a\/b;<br>return (r);<br>}<br><br>int main () {<br>cout &lt;&lt; divide (12);<br>cout &lt;&lt; endl;<br>cout &lt;&lt; divide (20,4);<br>return 0;<br>}<\/td><td>6<br>5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728\u7a0b\u5e8f\u4e2d\u6709\u4e24\u6b21\u8c03\u7528\u51fd\u6570<strong>divide<\/strong>\u3002\u7b2c\u4e00\u6b21\u8c03\u7528\uff1a<code>divide (12)<\/code><\/p>\n\n\n\n<p>\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u88ab\u6307\u660e\uff0c\u4f46\u51fd\u6570<strong>divide<\/strong>\u5141\u8bb8\u6709\u4e24\u4e2a\u53c2\u6570\u3002\u56e0\u6b64\u51fd\u6570<strong>divide<\/strong>&nbsp;\u5047\u8bbe\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u503c\u4e3a<strong>2<\/strong>\uff0c\u56e0\u4e3a\u6211\u4eec\u5df2\u7ecf\u5b9a\u4e49\u4e86\u5b83\u4e3a\u8be5\u53c2\u6570\u7f3a\u7701\u7684\u9ed8\u8ba4\u503c(\u6ce8\u610f\u51fd\u6570\u58f0\u660e\u4e2d\u7684int b=2)\u3002\u56e0\u6b64\u8fd9\u6b21\u51fd\u6570\u8c03\u7528\u7684\u7ed3\u679c\u662f&nbsp;<strong>6<\/strong>&nbsp;(12\/2)\u3002<\/p>\n\n\n\n<p>\u5728\u7b2c\u4e8c\u6b21\u8c03\u7528\u4e2d\uff1a<code>divide (20,4)<\/code><\/p>\n\n\n\n<p>\u8fd9\u91cc\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u6240\u4ee5\u9ed8\u8ba4\u503c (int b=2) \u88ab\u4f20\u5165\u7684\u53c2\u6570\u503c4\u6240\u53d6\u4ee3\uff0c\u4f7f\u5f97\u6700\u540e\u7ed3\u679c\u4e3a 5 (20\/4).<\/p>\n\n\n\n<h3>\u51fd\u6570\u91cd\u8f7d(Overloaded functions)<\/h3>\n\n\n\n<p>\u4e24\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u53ef\u4ee5\u7528\u540c\u6837\u7684\u540d\u5b57\uff0c\u53ea\u8981\u5b83\u4eec\u7684\u53c2\u91cf(arguments)\u7684\u539f\u578b(prototype)\u4e0d\u540c\uff0c\u4e5f\u5c31\u662f\u8bf4\u4f60\u53ef\u4ee5\u628a\u540c\u4e00\u4e2a\u540d\u5b57\u7ed9\u591a\u4e2a\u51fd\u6570\uff0c\u5982\u679c\u5b83\u4eec\u7528\u4e0d\u540c\u6570\u91cf\u7684\u53c2\u6570\uff0c\u6216\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ overloaded function<br>#include &lt;iostream.h&gt;<br><br>int divide (int a, int b) {<br>return (a\/b);<br>}<br><br>float divide (float a, float b) {<br>return (a\/b);<br>}<br><br>int main () {<br>int x=5,y=2;<br>float n=5.0,m=2.0;<br>cout &lt;&lt; divide (x,y);<br>cout &lt;&lt; &#8220;\\n&#8221;;<br>cout &lt;&lt; divide (n,m);<br>cout &lt;&lt; &#8220;\\n&#8221;;<br>return 0;<br>}<\/td><td>2<br>2.5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\uff0c\u6211\u4eec\u7528\u540c\u4e00\u4e2a\u540d\u5b57\u5b9a\u4e49\u4e86\u4e24\u4e2a\u4e0d\u540c\u51fd\u6570\uff0c\u5f53\u5b83\u4eec\u5176\u4e2d\u4e00\u4e2a\u63a5\u53d7\u4e24\u4e2a\u6574\u578b(int)\u53c2\u6570\uff0c\u53e6\u4e00\u4e2a\u5219\u63a5\u53d7\u4e24\u4e2a\u6d6e\u70b9\u578b(float)\u53c2\u6570\u3002\u7f16\u8bd1\u5668 (compiler)\u901a\u8fc7\u68c0\u67e5\u4f20\u5165\u7684\u53c2\u6570\u7684\u7c7b\u578b\u6765\u786e\u5b9a\u662f\u54ea\u4e00\u4e2a\u51fd\u6570\u88ab\u8c03\u7528\u3002\u5982\u679c\u8c03\u7528\u4f20\u5165\u7684\u662f\u4e24\u4e2a\u6574\u6570\u53c2\u6570\uff0c\u90a3\u4e48\u662f\u539f\u578b\u5b9a\u4e49\u4e2d\u6709\u4e24\u4e2a\u6574\u578b(int)\u53c2\u91cf\u7684\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u5982\u679c\u4f20\u5165\u7684\u662f\u4e24\u4e2a\u6d6e\u70b9\u6570\uff0c\u90a3\u4e48\u662f\u539f\u578b\u5b9a\u4e49\u4e2d\u6709\u4e24\u4e2a\u6d6e\u70b9\u578b(float)\u53c2\u91cf\u7684\u51fd\u6570\u88ab\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u8fd9\u91cc\u6211\u4eec\u7528\u7684\u4e24\u4e2a\u51fd\u6570\u7684\u4ee3\u7801\u76f8\u540c\uff0c\u4f46\u8fd9\u5e76\u4e0d\u662f\u5fc5\u987b\u7684\u3002\u4f60\u53ef\u4ee5\u8ba9\u4e24\u4e2a\u51fd\u6570\u7528\u540c\u4e00\u4e2a\u540d\u5b57\u540c\u65f6\u5b8c\u6210\u5b8c\u5168\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h3>Inline \u51fd\u6570\uff08inline functions\uff09<\/h3>\n\n\n\n<p><strong>inline<\/strong>&nbsp;\u6307\u4ee4\u53ef\u4ee5\u88ab\u653e\u5728\u51fd\u6570\u58f0\u660e\u4e4b\u524d\uff0c\u8981\u6c42\u8be5\u51fd\u6570\u5fc5\u987b\u5728\u88ab\u8c03\u7528\u7684\u5730\u65b9\u4ee5\u4ee3\u7801\u5f62\u5f0f\u88ab\u7f16\u8bd1\u3002\u8fd9\u76f8\u5f53\u4e8e\u4e00\u4e2a\u5b8f\u5b9a\u4e49(macro)\u3002\u5b83\u7684\u597d\u5904\u53ea\u5bf9\u77ed\u5c0f\u7684\u51fd\u6570\u6709\u6548\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u56e0\u4e3a\u907f\u514d\u4e86\u8c03\u7528\u51fd\u6570\u7684\u4e00\u4e9b\u5e38\u89c4\u64cd\u4f5c\u7684\u65f6\u95f4(overhead)\uff0c\u5982\u53c2\u6570\u5806\u6808\u64cd\u4f5c\u7684\u65f6\u95f4\uff0c\u6240\u4ee5\u7f16\u8bd1\u7ed3\u679c\u7684\u8fd0\u884c\u4ee3\u7801\u4f1a\u66f4\u5feb\u4e00\u4e9b\u3002<\/p>\n\n\n\n<p>\u5b83\u7684\u58f0\u660e\u5f62\u5f0f\u662f\uff1a<code>inline type name ( arguments ... ) { instructions ... }<\/code><\/p>\n\n\n\n<p>\u5b83\u7684\u8c03\u7528\u548c\u5176\u4ed6\u7684\u51fd\u6570\u8c03\u7528\u4e00\u6837\u3002\u8c03\u7528\u51fd\u6570\u7684\u65f6\u5019\u5e76\u4e0d\u9700\u8981\u5199\u5173\u952e\u5b57inline \uff0c\u53ea\u6709\u5728\u51fd\u6570\u58f0\u660e\u524d\u9700\u8981\u5199\u3002<\/p>\n\n\n\n<h3>\u9012\u5f52\uff08Recursivity\uff09<\/h3>\n\n\n\n<p>\u9012\u5f52(recursivity)\u6307\u51fd\u6570\u5c06\u88ab\u81ea\u5df1\u8c03\u7528\u7684\u7279\u70b9\u3002\u5b83\u5bf9\u6392\u5e8f(sorting)\u548c\u9636\u4e58(factorial)\u8fd0\u7b97\u5f88\u6709\u7528\u3002\u4f8b\u5982\u8981\u83b7\u5f97\u4e00\u4e2a\u6570\u5b57n\u7684\u9636\u4e58\uff0c\u5b83\u7684\u6570\u5b66\u516c\u5f0f\u662f\uff1a<code>n! = n * (n-1) * (n-2) * (n-3) ... * 1<\/code><\/p>\n\n\n\n<p>\u66f4\u5177\u4f53\u4e00\u4e9b\uff0c5! (factorial of 5) \u662f\uff1a<code>5! = 5 * 4 * 3 * 2 * 1 = 120<\/code><\/p>\n\n\n\n<p>\u800c\u7528\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e2a\u8fd0\u7b97\u5c06\u5982\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ factorial calculator<br>#include &lt;iostream.h&gt;<br><br>long factorial (long a){<br>if (a &gt; 1) return (a * factorial (a-1));<br>else return (1);<br>}<br><br>int main () {<br>long l;<br>cout &lt;&lt; &#8220;Type a number: &#8220;;<br>cin &gt;&gt; l;<br>cout &lt;&lt; &#8220;!&#8221; &lt;&lt; l &lt;&lt; &#8221; = &#8221; &lt;&lt; factorial (l);<br>return 0;<br>}<\/td><td>Type a number: 9<br>!9 = 362880<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6ce8\u610f\u6211\u4eec\u5728\u51fd\u6570factorial\u4e2d\u662f\u600e\u6837\u8c03\u7528\u5b83\u81ea\u5df1\u7684\uff0c\u4f46\u53ea\u662f\u5728\u53c2\u6570\u503c\u5927\u4e8e1\u7684\u65f6\u5019\u624d\u505a\u8c03\u7528\uff0c\u56e0\u4e3a\u5426\u5219\u51fd\u6570\u4f1a\u8fdb\u5165\u6b7b\u5faa\u73af(an infinite recursive loop)\uff0c\u5f53\u53c2\u6570\u5230\u8fbe0\u7684\u65f6\u5019\uff0c\u51fd\u6570\u4e0d\u7ee7\u7eed\u7528\u8d1f\u6570\u4e58\u4e0b\u53bb(\u6700\u7ec8\u53ef\u80fd\u5bfc\u81f4\u8fd0\u884c\u65f6\u7684\u5806\u6808\u6ea2\u51fa\u9519\u8bef(stack overflow error)\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u6709\u4e00\u5b9a\u7684\u5c40\u9650\u6027\uff0c\u4e3a\u7b80\u5355\u8d77\u89c1\uff0c\u51fd\u6570\u8bbe\u8ba1\u4e2d\u4f7f\u7528\u7684\u6570\u636e\u7c7b\u578b\u4e3a\u957f\u6574\u578b(long)\u3002\u5728\u5b9e\u9645\u7684\u6807\u51c6\u7cfb\u7edf\u4e2d\uff0c\u957f\u6574\u578blong\u65e0\u6cd5\u5b58\u50a812\uff01\u4ee5\u4e0a\u7684\u9636\u4e58\u503c\u3002<\/p>\n\n\n\n<h3>\u51fd\u6570\u7684\u58f0\u660e\uff08Declaring functions\uff09<\/h3>\n\n\n\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5b9a\u4e49\u7684\u6240\u6709\u51fd\u6570\u90fd\u662f\u5728\u5b83\u4eec\u7b2c\u4e00\u6b21\u88ab\u8c03\u7528\uff08\u901a\u5e38\u662f\u5728main\u4e2d\uff09\u4e4b\u524d\uff0c\u800c\u628amain \u51fd\u6570\u653e\u5728\u6700\u540e\u3002\u5982\u679c\u91cd\u590d\u4ee5\u4e0a\u51e0\u4e2a\u4f8b\u5b50\uff0c\u4f46\u628amain \u51fd\u6570\u653e\u5728\u5176\u5b83\u88ab\u5b83\u8c03\u7528\u7684\u51fd\u6570\u4e4b\u524d\uff0c\u4f60\u5c31\u4f1a\u9047\u5230\u7f16\u8bd1\u9519\u8bef\u3002\u539f\u56e0\u662f\u5728\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u4e4b\u524d\uff0c\u51fd\u6570\u5fc5\u987b\u5df2\u7ecf\u88ab\u5b9a\u4e49\u4e86\uff0c\u5c31\u50cf\u6211\u4eec\u524d\u9762\u4f8b\u5b50\u4e2d\u6240\u505a\u7684\u3002<\/p>\n\n\n\n<p>\u4f46\u5b9e\u9645\u4e0a\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\u6765\u907f\u514d\u5728main \u6216\u5176\u5b83\u51fd\u6570\u4e4b\u524d\u5199\u51fa\u6240\u6709\u88ab\u4ed6\u4eec\u8c03\u7528\u7684\u51fd\u6570\u7684\u4ee3\u7801\uff0c\u90a3\u5c31\u662f\u5728\u4f7f\u7528\u524d\u5148\u58f0\u660e\u51fd\u6570\u7684\u539f\u578b\u5b9a\u4e49\u3002\u58f0\u660e\u51fd\u6570\u5c31\u662f\u5bf9\u51fd\u6570\u5728\u7684\u5b8c\u6574\u5b9a\u4e49\u4e4b\u524d\u505a\u4e00\u4e2a\u77ed\u5c0f\u91cd\u8981\u7684\u58f0\u660e\uff0c\u4ee5\u4fbf\u8ba9\u7f16\u8bd1\u5668\u77e5\u9053\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u5b83\u7684\u5f62\u5f0f\u662f\uff1a<code>type name ( argument_type1, argument_type2, ...);<\/code><\/p>\n\n\n\n<p>\u5b83\u4e0e\u4e00\u4e2a\u51fd\u6570\u7684\u5934\u5b9a\u4e49\uff08header definition\uff09\u4e00\u6837\uff0c\u9664\u4e86\uff1a<\/p>\n\n\n\n<ul><li>\u5b83\u4e0d\u5305\u62ec\u51fd\u6570\u7684\u5185\u5bb9\uff0c \u4e5f\u5c31\u662f\u5b83\u4e0d\u5305\u62ec\u51fd\u6570\u540e\u9762\u82b1\u62ec\u53f7{}\u5185\u7684\u6240\u6709\u8bed\u53e5\u3002<\/li><li>\u5b83\u4ee5\u4e00\u4e2a\u5206\u53f7semicolon sign (;) \u7ed3\u675f\u3002<\/li><li>\u5728\u53c2\u6570\u5217\u4e3e\u4e2d\u53ea\u9700\u8981\u5199\u51fa\u5404\u4e2a\u53c2\u6570\u7684\u6570\u636e\u7c7b\u578b\u5c31\u591f\u4e86\uff0c\u81f3\u4e8e\u6bcf\u4e2a\u53c2\u6570\u7684\u540d\u5b57\u53ef\u4ee5\u5199\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u5199\uff0c\u4f46\u662f\u6211\u4eec\u5efa\u8bae\u5199\u4e0a\u3002<\/li><\/ul>\n\n\n\n<p>\u4f8b\u5982\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ \u58f0\u660e\u51fd\u6570\u539f\u578b<br>#include &lt;iostream.h&gt;<br><br>void odd (int a);<br>void even (int a);<br><br>int main () {<br>int i;<br>do {<br>cout &lt;&lt; &#8220;Type a number: (0 to exit)&#8221;;<br>cin &gt;&gt; i;<br>odd (i);<br>} while (i!=0);<br>return 0;<br>}<br><br>void odd (int a) {<br>if ((a%2)!=0) cout &lt;&lt; &#8220;Number is odd.\\n&#8221;;<br>else even (a);<br>}<br><br>void even (int a) {<br>if ((a%2)==0) cout &lt;&lt; &#8220;Number is even.\\n&#8221;;<br>else odd (a);<br>}<\/td><td>Type a number (0 to exit): 9<br>Number is odd.<br>Type a number (0 to exit): 6<br>Number is even.<br>Type a number (0 to exit): 1030<br>Number is even.<br>Type a number (0 to exit): 0<br>Number is even.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u7684\u786e\u4e0d\u662f\u5f88\u6709\u6548\u7387\uff0c\u6211\u76f8\u4fe1\u73b0\u5728\u4f60\u5df2\u7ecf\u53ef\u4ee5\u53ea\u7528\u4e00\u534a\u884c\u6570\u7684\u4ee3\u7801\u6765\u5b8c\u6210\u540c\u6837\u7684\u529f\u80fd\u3002\u4f46\u8fd9\u4e2a\u4f8b\u5b50\u663e\u793a\u4e86\u51fd\u6570\u539f\u578b\uff08prototyping functions\uff09\u662f\u600e\u6837\u5de5\u4f5c\u7684\u3002\u5e76\u4e14\u5728\u8fd9\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u4e2d\uff0c\u4e24\u4e2a\u51fd\u6570\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a\u662f\u5fc5\u987b\u5b9a\u4e49\u539f\u578b\u7684\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u9996\u5148\u770b\u5230\u7684\u662f\u51fd\u6570odd \u548ceven\u7684\u539f\u578b\uff1avoid odd (int a);<br>void even (int a);<\/p>\n\n\n\n<p>\u8fd9\u6837\u4f7f\u5f97\u8fd9\u4e24\u4e2a\u51fd\u6570\u53ef\u4ee5\u5728\u5b83\u4eec\u88ab\u5b8c\u6574\u5b9a\u4e49\u4e4b\u524d\u5c31\u88ab\u4f7f\u7528\uff0c\u4f8b\u5982\u5728main\u4e2d\u88ab\u8c03\u7528\uff0c\u8fd9\u6837main\u5c31\u53ef\u4ee5\u88ab\u653e\u5728\u903b\u8f91\u4e0a\u66f4\u5408\u7406\u7684\u4f4d\u7f6e\uff1a\u5373\u7a0b\u5e8f\u4ee3\u7801\u7684\u5f00\u5934\u90e8\u5206\u3002<\/p>\n\n\n\n<p>\u5c3d\u7ba1\u5982\u6b64\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u9700\u8981\u81f3\u5c11\u4e00\u4e2a\u51fd\u6570\u539f\u578b\u5b9a\u4e49\u7684\u7279\u6b8a\u539f\u56e0\u662f\u56e0\u4e3a\u5728odd \u51fd\u6570\u91cc\u9700\u8981\u8c03\u7528even \u51fd\u6570\uff0c\u800c\u5728even \u51fd\u6570\u91cc\u4e5f\u540c\u6837\u9700\u8981\u8c03\u7528odd\u51fd\u6570\u3002\u5982\u679c\u4e24\u4e2a\u51fd\u6570\u4efb\u4f55\u4e00\u4e2a\u90fd\u6ca1\u88ab\u63d0\u524d\u5b9a\u4e49\u539f\u578b\u7684\u8bdd\uff0c\u5c31\u4f1a\u51fa\u73b0\u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3a\u6216\u8005odd \u5728even \u51fd\u6570\u4e2d\u662f\u4e0d\u53ef\u89c1\u7684(\u56e0\u4e3a\u5b83\u8fd8\u6ca1\u6709\u88ab\u5b9a\u4e49)\uff0c\u6216\u8005even \u51fd\u6570\u5728odd\u51fd\u6570\u4e2d\u662f\u4e0d\u53ef\u89c1\u7684\u3002<\/p>\n\n\n\n<p>\u5f88\u591a\u7a0b\u5e8f\u5458\u5efa\u8bae\u7ed9\u6240\u6709\u7684\u51fd\u6570\u5b9a\u4e49\u539f\u578b\u3002\u8fd9\u4e5f\u662f\u6211\u7684\u5efa\u8bae\uff0c\u7279\u522b\u662f\u5728\u6709\u5f88\u591a\u51fd\u6570\u6216\u51fd\u6570\u5f88\u957f\u7684\u60c5\u51b5\u4e0b\u3002\u628a\u6240\u6709\u51fd\u6570\u7684\u539f\u578b\u5b9a\u4e49\u653e\u5728\u4e00\u4e2a\u5730\u65b9\uff0c\u53ef\u4ee5\u4f7f\u6211\u4eec\u5728\u51b3\u5b9a\u600e\u6837\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u7684\u65f6\u5019\u8f7b\u677e\u4e00\u4e9b\uff0c\u540c\u65f6\u4e5f\u6709\u52a9\u4e8e\u751f\u6210\u5934\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>2.3 \u51fd\u6570II\uff08Functions II\uff09 \u53c2\u6570\u6309\u6570\u503c\u4f20\u9012\u548c\u6309\u5730\u5740\u4f20\u9012(Arguments passed by value and by reference) \u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u770b\u5230\u7684\u6240\u6709\u51fd\u6570\u4e2d\uff0c\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u7684\u53c2\u6570\u5168\u90e8\u662f\u6309\u6570\u503c\u4f20\u9012\u7684(by value)\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u6211\u4eec\u8c03\u7528\u4e00\u4e2a\u5e26\u6709\u53c2\u6570\u7684\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u4f20\u9012\u5230\u51fd\u6570\u4e2d\u7684\u662f\u53d8\u91cf\u7684\u6570\u503c\u800c\u4e0d\u662f\u53d8\u91cf\u672c\u8eab\u3002 \u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u8c03\u7528\u6211\u4eec\u7684\u7b2c\u4e00\u4e2a\u51fd\u6570addition \uff1aint x=5, y=3, z;z = addition ( x , y ); \u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\u6211\u4eec\u8c03\u7528\u51fd\u6570addition \u540c\u65f6\u5c06x\u548cy\u7684\u503c\u4f20\u7ed9\u5b83\uff0c\u5373\u5206\u522b\u4e3a5\u548c3\uff0c\u800c\u4e0d\u662f\u4e24\u4e2a\u53d8\u91cf\uff1a \u8fd9\u6837\uff0c\u5f53\u51fd\u6570addition\u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u7684\u53d8\u91cfa\u548cb\u7684\u503c\u5206\u522b\u53d8\u4e3a5\u548c3\uff0c\u4f46\u5728\u51fd\u6570addition\u5185\u5bf9\u53d8\u91cfa \u6216b \u6240\u505a\u7684\u4efb\u4f55\u4fee\u6539\u4e0d\u4f1a\u5f71\u54cd\u53d8\u91cf\u4ed6\u5916\u9762\u7684\u53d8\u91cfx \u548c y \u7684\u503c\uff0c\u56e0\u4e3a\u53d8\u91cfx\u548cy\u5e76\u6ca1\u6709\u628a\u5b83\u4eec\u81ea\u5df1\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u800c\u53ea\u662f\u4f20\u9012\u4e86\u4ed6\u4eec\u7684\u6570\u503c\u3002 \u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f60\u53ef\u80fd\u9700\u8981\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u63a7\u5236\u4e00\u4e2a\u51fd\u6570\u4ee5\u5916\u7684\u53d8\u91cf\u3002\u8981\u5b9e\u73b0\u8fd9\u79cd\u64cd\u4f5c\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528\u6309\u5730\u5740\u4f20\u9012\u7684\u53c2\u6570\uff08arguments passed by reference\uff09\uff0c\u5c31\u8c61\u4e0b\u9762\u4f8b\u5b50\u4e2d\u7684\u51fd\u6570duplicate\uff1a \/\/ passing parameters by reference#include &lt;iostream.h&gt; void duplicate (int&amp; a, int&amp; b, int&amp; c){a*=2;b*=2;c*=2;} int main (){int x=1, y=3, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":325,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/331"}],"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=331"}],"version-history":[{"count":1,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/331\/revisions"}],"predecessor-version":[{"id":332,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/331\/revisions\/332"}],"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=331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}