Getting Started

(lily) #1

Chapter 6: C Functions and Program Structures


nd w ave a seless function. If we want to add 1 and 1, we
st add hem.

ariab s E r


written:

igned char, unsigned char);
unsigned char results = 0;

adder(add1,add2);

) getrewarded();
();

d a har ad1, unsigned char a1)

a2;

t
changed it to 3. Then when the interrupt finishes and we

ntly occupy memory, while defining
esults emory when adder is called, and release the
memor


char adder(unsigned char ad1, unsigned char a1)
{
return a1 + a2;
}


A e h concise and totally u
ju t


V le xternal, Static, and Registe


Another way to do the adder() thing would be to use and external variable
(global). These are variables defined outside any function, usually in a header or
before main() and are available for any function to use. We could have


void adder(uns


int main()
{
unsigned char add1 = 1;
unsigned char add2 = 1;


if(results == 2
else getboinked
}


voi dder(unsigned c
{
results = a1 +
}


Which would work fine. Unless of course an interrupt triggered right after we se
results in adder() and
look at results in main() we get boinked again. This is a good reason to avoid
external variables. You never know where they’ve been or what kind of nasty stuff
they might track in. Also they permane
‘r ’ in adder would only use m
y when finished.

Free download pdf