{"id":7178,"date":"2021-12-24T06:24:54","date_gmt":"2021-12-24T06:24:54","guid":{"rendered":"https:\/\/www.qec.tw\/?p=7178"},"modified":"2021-12-28T09:26:08","modified_gmt":"2021-12-28T09:26:08","slug":"functions","status":"publish","type":"post","link":"https:\/\/www.qec.tw\/zh\/86duino-reference\/functions\/","title":{"rendered":"\u529f\u80fd"},"content":{"rendered":"<p class=\"wp-block-paragraph\">\u5c07\u7a0b\u5f0f\u78bc\u5206\u5272\u6210\u591a\u500b\u51fd\u5f0f\u5340\u584a\u53ef\u4ee5\u8b93\u7a0b\u5f0f\u8a2d\u8a08\u5e2b\u66f4\u5bb9\u6613\u5c07\u7a0b\u5f0f\u78bc\u6a21\u7d44\u5316\uff0c\u7a0b\u5f0f\u8a2d\u8a08\u5e2b\u53ef\u4ee5\u5c07\u4efb\u52d9\u9084\u6709\u56de\u50b3\u503c\u5b9a\u7fa9\u5728\u4e00\u500b\u51fd\u5f0f\u5340\u584a\u4e2d\uff0c\u518d\u8b93\u5176\u4ed6\u7a0b\u5f0f\u4f86 \u201d\u547c\u53eb\u201d \u5b83\u5011\u3002\u50b3\u7d71\u7684\u7528\u6cd5\u662f\uff0c\u7576\u5728\u7a0b\u5f0f\u78bc\u5167\u9700\u8981\u591a\u6b21\u67d0\u7a2e\u76f8\u540c\u52d5\u4f5c\u6642\u5c31\u5275\u5efa\u4e00\u500b\u51fd\u5f0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7d66\u7fd2\u6163\u7528 BASIC \u7684\u7a0b\u5f0f\u8a2d\u8a08\u5e2b\uff0c\u5728 86Duino \u4e2d\u7684\u51fd\u5f0f\u63d0\u4f9b (\u9084\u6709\u64f4\u5145) \u4e86\u4e00\u4e9b\u5b50\u7a0b\u5f0f (BASIC \u4e2d\u7684 GOSUB)\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c07\u7a0b\u5f0f\u78bc\u5206\u5272\u6210\u51fd\u5f0f\u5177\u6709\u4e0b\u5217\u5e7e\u500b\u512a\u9ede\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u51fd\u5f0f\u8b93\u7a0b\u5f0f\u78bc\u672c\u8eab\u66f4\u5177\u7d44\u7e54\u6027\uff0c\u4e5f\u8b93\u7a0b\u5f0f\u78bc\u8b8a\u7684\u8f03\u6982\u5ff5\u5316\u3002<\/li><li>\u51fd\u5f0f\u8b93\u4e00\u500b\u52d5\u4f5c\u88ab\u64b0\u5beb\u5728\u4e00\u500b\u5730\u65b9\uff0c\u6240\u4ee5\u9019\u500b\u51fd\u5f0f\u53ea\u9700\u8981\u601d\u8003\u8207\u9664\u932f\u4e00\u6b21\u5c31\u53ef\u4ee5\u4e86\u3002<\/li><li>\u9019\u4e5f\u6e1b\u5c11\u5728\u4fee\u6539\u7a0b\u5f0f\u78bc\u904e\u7a0b\u4e2d\u767c\u751f\u932f\u8aa4\u7684\u6a5f\u7387\u3002<\/li><li>\u51fd\u5f0f\u8b93\u6574\u500b\u7a0b\u5f0f\u78bc\u8b8a\u7684\u6bd4\u8f03\u5c0f\u4e14\u7dca\u5bc6\uff0c\u56e0\u70ba\u518d\u5229\u7528\u7387\u8b8a\u9ad8\u4e86\u3002<\/li><li>\u9019\u6a23\u8b93\u518d\u6b21\u5229\u7528\u5225\u7684\u7a0b\u5f0f\u78bc\u8b8a\u5f97\u66f4\u5bb9\u6613\u4e5f\u66f4\u6a21\u7d44\u5316\uff0c\u526f\u4f5c\u7528\u66f4\u5c11\uff0c\u9084\u6709\u53ef\u8b80\u6027\u8b8a\u66f4\u597d\u3002<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph translation-block\"><code>setup()<\/code> \u8207 <code>loop()<\/code> \u662f Aruino \u53ca 86Duino \u5fc5\u5099\u7684\u5169\u500b\u51fd\u5f0f\uff0c\u5176\u4ed6\u7684\u51fd\u5f0f\u5fc5\u9808\u5275\u5efa\u5728\u9019\u5169\u500b\u51fd\u5f0f\u4e4b\u5916\u3002\u8209\u500b\u4f8b\u5b50\uff0c\u6211\u5011\u5efa\u7acb\u4e00\u500b\u7c21\u55ae\u7684\u51fd\u5f0f\u53bb\u5c07\u5169\u500b\u6578\u5b57\u76f8\u4e58\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b<\/h2>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"http:\/\/www.86duino.com\/wp-content\/uploads\/2013\/12\/FuncAnatomy.png\" alt=\"FuncAnatomy\" width=\"413\" height=\"275\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u201c\u547c\u53eb\u201d \u4e00\u500b\u7c21\u55ae\u7684\u4e58\u6cd5\u51fd\u5f0f\uff0c\u6211\u5011\u50b3\u7d66\u5b83\u9810\u60f3\u4e2d\u7684\u8cc7\u6599\u5f62\u614b\u53c3\u6578\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>void loop{\n  int i = 2;\n  int j = 3;\n  int k;\n \n  k = myMultiplyFunction(i, j); \/\/ k now contains 6\n}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph translation-block\">\u51fd\u5f0f\u9700\u8981\u88ab\u5ba3\u544a\u5728\u4efb\u4f55\u7684\u51fd\u5f0f\u4e4b\u5916\uff0c\u56e0\u6b64 \u201c<code>myMultiplyFunction()<\/code>\u201d \u8981\u88ab\u5ba3\u544a\u5728 \u201d<code>loop()<\/code>\u201d \u4e0a\u9762\u6216\u8005\u4e0b\u9762\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6574\u500b\u7a0b\u5f0f\u78bc\u6703\u50cf\u9019\u6a23\uff1a<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>void setup(){\n  Serial.begin(9600);\n}\n \nvoid loop() {\n  int i = 2;\n  int j = 3;\n  int k;\n \n  k = myMultiplyFunction(i, j); \/\/ k now contains 6\n  Serial.println(k);\n  delay(500);\n}\n \nint myMultiplyFunction(int x, int y){\n  int result;\n  result = x * y;\n  return result;\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u5176\u4ed6\u7bc4\u4f8b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph translation-block\">\u9019\u500b\u51fd\u5f0f\u6703\u85c9\u7531 <code>analogRead()<\/code> \u8b80\u53d6\u4e00\u500b\u611f\u6e2c\u5668\u4e94\u6b21\u4e26\u4e14\u8a08\u7b97\u4e94\u6b21\u8b80\u53d6\u503c\u7684\u5e73\u5747\u3002\u5b83\u628a\u8cc7\u6599\u9650\u5236\u5728 8 bit \u4e4b\u4e2d (0 \u5230 255)\uff0c\u4e26\u56de\u50b3\u8a08\u7b97\u5f8c\u7684\u7d50\u679c\u3002<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>int ReadSens_and_Condition(){\n  int i;\n  int sval = 0;\n \n  for (i = 0; i &lt; 5; i++){\n    sval = sval + analogRead(0);    \/\/ sensor on analog pin 0\n  }\n \n  sval = sval \/ 5;    \/\/ average\n  sval = sval \/ 4;    \/\/ scale to 8 bits (0 - 255)\n  sval = 255 - sval;  \/\/ invert output\n  return sval;\n}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u547c\u53eb\u6211\u5011\u7684\u51fd\u5f0f\u53ea\u9700\u8981\u6307\u6d3e\u4e00\u500b\u8b8a\u6578\u7d66\u5b83\u3002<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>int sens;\n \nsens = ReadSens_and_Condition();<\/pre><\/div>\n\n\n<hr \/>\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.qec.tw\/zh\/86duino\/86duino-language-reference\/\">\u8a9e\u6cd5\u53c3\u8003\u4e3b\u9801\u9762<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph translation-block\"><em>86Duino \u53c3\u8003\u7684\u6587\u672c\u662f\u6839\u64da <a href=\"https:\/\/creativecommons.org\/licenses\/by-sa\/3.0\/\" target=\"_self\">\u77e5\u8b58\u5171\u4eab\u7f72\u540d-\u76f8\u540c\u65b9\u5f0f\u5206\u4eab 3.0 \u8a31\u53ef\u8b49<\/a>\uff0c\u90e8\u5206\u6587\u672c\u662f\u5f9e <a href=\"https:\/\/arduino.cc\/en\/Reference\/HomePage\" target=\"_self\">Arduino \u53c3\u8003<\/a> \u4fee\u6539\u7684\u3002 \u53c3\u8003\u4e2d\u7684\u4ee3\u78bc\u793a\u4f8b\u5df2\u767c\u4f48\u5230\u516c\u5171\u9818\u57df\u3002<\/em><\/p>","protected":false},"excerpt":{"rendered":"<p>Segmenting code into functions allows a programmer to create modular pieces of code that perform a defined task and then return to the area of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7179,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[195],"tags":[162],"class_list":["post-7178","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-86duino-reference","tag-86duino"],"_links":{"self":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/7178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/comments?post=7178"}],"version-history":[{"count":0,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/7178\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media\/7179"}],"wp:attachment":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media?parent=7178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/categories?post=7178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/tags?post=7178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}