{"id":7173,"date":"2021-12-24T02:46:43","date_gmt":"2021-12-24T02:46:43","guid":{"rendered":"https:\/\/www.qec.tw\/?p=7173"},"modified":"2021-12-28T09:26:08","modified_gmt":"2021-12-28T09:26:08","slug":"variables","status":"publish","type":"post","link":"https:\/\/www.qec.tw\/zh\/86duino-reference\/variables\/","title":{"rendered":"\u8b8a\u6578"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A variable is a place to store a piece of data. It has a name, a value, and a type. For example, this statement (called a\u00a0<em>declaration<\/em>):<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>int pin = 13;<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">creates a variable whose name is\u00a0<code>pin<\/code>, whose value is 13, and whose type is\u00a0<code>int<\/code>. Later on in the program, you can refer to this variable by its name, at which point its value will be looked up and used. For example, in this statement:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>pinMode(13, OUTPUT);<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The advantage of a variable in this case is that you only need to specify the actual number of the pin once, but you can use it lots of times. So if you later decide to change from pin 13 to pin 12, you only need to change one spot in the code. Also, you can use a descriptive name to make the significance of the variable clear (e.g. a program controlling an RGB LED might have variables called redPin, greenPin, and bluePin).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A variable has other advantages over a value like a number. Most importantly, you can change the value of a variable using an\u00a0<em>assignment<\/em>\u00a0(indicated by an equals sign). For example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>pin = 12;<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">will change the value of the variable to 12. Notice that we don\u2019t specify the type of the variable: it\u2019s not changed by the assignment. That is, the name of the variable is permanently associated with a type; only its value changes. (Remarks: In some languages, like Python, types are associated with values, not variable names, and you can assign values of any type to a variable. This is referred to as\u00a0<em>dynamic typing<\/em>. Dynamic typing is not supported by the both Arduino language and 86Duino language)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that you have to declare a variable before you can assign a value to it. If you include the preceding statement in a program without the first statement above, you\u2019ll get a message like: \u201cerror: pin was not declared in this scope\u201d.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you assign one variable to another, you\u2019re making a copy of its value and storing that copy in the location in memory associated with the other variable. Changing one has no effect on the other. For example, after:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>int pin = 13;\nint pin2 = pin;\npin = 12;<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">only\u00a0<code>pin<\/code>\u00a0has the value 12;\u00a0<code>pin2<\/code>\u00a0is still 13.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now what, you might be wondering, did the word \u201cscope\u201d in that error message above mean? It refers to the part of your program in which the variable can be used. This is determined by where you declare it. For example, if you want to be able to use a variable anywhere in your program, you can declare at the top of your code. This is called a\u00a0<em>global<\/em>\u00a0variable; here\u2019s an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>int pin = 13;\n \nvoid setup()\n{\n  pinMode(pin, OUTPUT);\n}\n \nvoid loop()\n{\n  digitalWrite(pin, HIGH);\n}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see,\u00a0<code>pin<\/code>\u00a0is used in both the\u00a0<code>setup()<\/code>\u00a0and\u00a0<code>loop()<\/code>\u00a0functions. Both functions are referring to the same variable, so that changing it one will affect the value it has in the other, as in:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>int pin = 13;\n \nvoid setup()\n{\n  pin = 12;\n  pinMode(pin, OUTPUT);\n}\n \nvoid loop()\n{\n  digitalWrite(pin, HIGH);\n}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the\u00a0<code>digitalWrite()<\/code>\u00a0function called from\u00a0<code>loop()<\/code>\u00a0will be passed a value of 12, since that\u2019s the value that was assigned to the variable in the\u00a0<code>setup()<\/code>\u00a0function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you only need to use a variable in a single function, you can declare it there, in which case its scope will be limited to that function. For example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>void setup()\n{\n  int pin = 13;\n  pinMode(pin, OUTPUT);\n  digitalWrite(pin, HIGH);\n}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In this case, the variable pin can only be used inside the\u00a0<code>setup()<\/code>\u00a0function. If you try to do something like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>void loop()\n{\n  digitalWrite(pin, LOW); \/\/ wrong: pin is not in scope here.\n}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">you\u2019ll get the same message as before: \u201cerror: \u2018pin\u2019 was not declared in this scope\u201d. That is, even though you\u2019ve declared pin somewhere in your program, you\u2019re trying to use it somewhere outside its scope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why, you might be wondering, wouldn\u2019t you make all your variables global? After all, if I don\u2019t know where I might need a variable, why should I limit its scope to just one function? The answer is that it can make it easier to figure out what happens to it. If a variable is global, its value could be changed anywhere in the code, meaning that you need to understand the whole program to know what will happen to the variable. For example, if your variable has a value you didn\u2019t expect, it can be much easier to figure out where the value came from if the variable has a limited scope.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">See also<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>[Language] <a href=\"http:\/\/www.86duino.com\/?p=2933\" target=\"_blank\" rel=\"noopener\">Variable Declaration<\/a><\/li><\/ul>\n\n\n<hr \/>\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.qec.tw\/86duino\/86duino-language-reference\/\">Language Reference Home<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em>The text of the 86Duino reference is a modification of&nbsp;<a href=\"http:\/\/arduino.cc\/en\/Reference\/HomePage\" target=\"_blank\" rel=\"noopener\">the Arduino reference<\/a>&nbsp;and is licensed under a&nbsp;<a href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/3.0\/\" target=\"_blank\" rel=\"noopener\">Creative Commons Attribution-ShareAlike 3.0 License<\/a>. Code samples in the reference are released into the public domain.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A variable is a place to store a piece of data. It has a name, a value, and a type. For example, this statement (called [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7174,"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-7173","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\/7173","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=7173"}],"version-history":[{"count":0,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/7173\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media\/7174"}],"wp:attachment":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media?parent=7173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/categories?post=7173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/tags?post=7173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}