Programming Arduino Getting Started with Sketches


void function, the  void



Yüklə 5,42 Kb.
Pdf görüntüsü
səhifə35/78
tarix22.12.2023
ölçüsü5,42 Kb.
#189286
1   ...   31   32   33   34   35   36   37   38   ...   78
Arduino SimonMonk 2011

void
function, the 
void
keyword is at the left margin, as is the opening curly brace on the next line, but then all the
text within the curly braces is indented. The amount of indentation does not really matter.
Some people use two spaces, some four. You can also press Tab to indent. In this book, we
use two spaces for indentation.
If you were to have an if statement inside a function definition, then once again you
would add two more spaces for the lines within the curly braces of the 
if
command, as in
the following example:
You might include another 
if
inside the first 
if
, which would add yet another level of
indentation, making six spaces from the left margin.
All of this might sound a bit trivial, but if you ever sort through someone else’s badly
formatted sketches, you will find it very difficult.
Opening Braces
There are two schools of thought as to where to put the first curly brace in a function
definition, 
if
statement, or 
for
loop. One way is to place the curly brace on the line after
the rest of the command, as we have in all the examples so far, or put it on the same line,
like this:


This style is most commonly used in the Java programming language, which shares much
of the same syntax as C. I prefer the first form, which seems to be the form most
commonly used in the Arduino world.
Whitespace
The compiler ignores spaces tabs and new lines, apart from using them as a way of
separating the “tokens” or words in your sketch. Thus the following example will compile
without a problem:
This will work, but good luck trying to read it.
Where assignments are made, some people will write the following:
But others will write the following:
Which of these two styles you use really does not matter, but it is a good idea to be
consistent. I use the first form.
Comments
Comments are text that is kept in your sketch along with all the real program code, but
which actually performs no programming function whatsoever. The sole purpose of
comments is to be a reminder to you or others as to why the code is written as it is. A
comment line may also be used to present a title.
The compiler will completely ignore any text that is marked as being a comment. We
have included comments as titles at the top of many of the sketches in the book so far.
There are two forms of syntax for comments:


• The single line comment that starts with 

Yüklə 5,42 Kb.

Dostları ilə paylaş:
1   ...   31   32   33   34   35   36   37   38   ...   78




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin