Programming Arduino Getting Started with Sketches


loop is run, the variable count



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

loop
is run, the variable
count
will be given the value 0 again, so 
count
will never reach 20 and the LED will just
keep flashing forever. The whole reason that we made 
count
a global in the first place was
so that its value would not be reset. The only place that we use 
count
is in the 
loop
function, so this is where it should be placed.


Fortunately, there is a mechanism in C that gets around this conundrum. It is the
keyword 
static
. When you use the keyword 
static
in front of a variable declaration in a
function, it has the effect of initializing the variable only the first time that the function is
run. Perfect! That’s just what is required in this situation. We can keep our variable in the
function where it’s used without it getting set back to 0 every time the function runs.
Sketch 4-04 shows this in operation:
Return Values
Computer science, as an academic discipline, has as its parents mathematics and
engineering. This heritage lingers on in many of the names associated with programming.
The word 
function
is itself a mathematical term. In mathematics, the input to the function
(the argument) completely determines the output. We have written functions that take an
input, but none that give us back a value. All our functions have been “void” functions. If
a function returns a value, then you specify a return type.
Let’s look at writing a function that takes a temperature in degrees Centigrade and
returns the equivalent in degrees Fahrenheit:


The function definition now starts with 
int
rather than 
void
to indicate that the function
will return an 

Yüklə 5,42 Kb.

Dostları ilə paylaş:
1   ...   27   28   29   30   31   32   33   34   ...   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