From 2d429e75ce69e77f8c95490ac03881ec9aa0354a Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Mon, 25 Oct 2010 21:15:28 -0400 Subject: arduino language reference nearing completion, properly CC-BY-SA 3.0 attributed --- source/arduino/scope.rst | 63 +++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 30 deletions(-) (limited to 'source/arduino/scope.rst') diff --git a/source/arduino/scope.rst b/source/arduino/scope.rst index bb56246..0e793ec 100644 --- a/source/arduino/scope.rst +++ b/source/arduino/scope.rst @@ -1,56 +1,59 @@ +.. highlight:: cpp + .. _arduino-scope: Variable Scope ============== -Variables in the C programming language, which Arduino uses, have a -property called *scope*. This is in contrast to languages such as -BASIC where every variable is a *global* variable. - - - -A global variable is one that can be *seen* by every function in a -program. Local variables are only visible to the function in which -they are declared. In the Arduino environment, any variable -declared outside of a function (e.g. setup(), loop(), etc. ), is a -global variable. - +Variables in the C++ programming language, which Maple uses (all of +your sketches are C++ programs in disguise), have a property called +*scope*. This is in contrast to languages such as BASIC where every +variable is a *global* variable. +A global variable is one that can be "seen" by every function in a +program. Local variables are only usable within the function in which +they are declared. In the :ref:`Maple IDE `, any variable +declared outside of a function (like :ref:`setup() `, +:ref:`loop() `, etc.), is a global variable. When programs start to get larger and more complex, local variables -are a useful way to insure that only one function has access to its -own variables. This prevents programming errors when one function +are a useful way to ensure that a function has exclusive access to its +own variables. This prevents programming errors when one function inadvertently modifies variables used by another function. +It is also sometimes useful to declare and initialize a variable +inside a :ref:`for ` loop. This creates a variable that +can only be accessed from inside the loop body. - -It is also sometimes handy to declare and initialize a variable -inside a *for* loop. This creates a variable that can only be -accessed from inside the for-loop brackets. - - +Scope in C++ is actually a fairly complex topic. More information is +available in the `C++ programming Wikibook +`_. Example: -------- :: - int gPWMval; // any function will see this variable - - void setup() - { + int globalVar; // any function will see this variable + + void setup() { // ... } - - void loop() - { + + void loop() { int i; // "i" is only "visible" inside of "loop" float f; // "f" is only "visible" inside of "loop" // ... - + for (int j = 0; j <100; j++){ - // variable j can only be accessed inside the for-loop brackets + // variable j can only be accessed inside the for-loop brackets + i = j * j; } - + i = globalVar; // globalVar can be accessed from anywhere, including loop() } +See Also +-------- + +- `C++ programming Wikibook `_. +- Wikipedia article on `scope `_ -- cgit v1.2.3